วันพฤหัสบดีที่ 13 ตุลาคม พ.ศ. 2559

Comic creator code (test)

# File name: drawingspace.py
import kivy
kivy.require('1.7.0')
from kivy.uix.relativelayout import RelativeLayout

class DrawingSpace(RelativeLayout):
    figureName = [None]
    def on_30_sep(self, instance, value):
        self.status_bar.counter = len(self.children)
        countStickman = 0
        for name in self.__class__.figureName:
            if(name == "Stickman"):
                countStickman += 1
        self.status_bar.stickCounter = countStickman
   
    def on_30_sep(self):
        self.status_bar.counter = len(self.children)   
        countStickman = 0
        for name in self.__class__.figureName:
            if(name == "Stickman"):
                countStickman += 1
        self.status_bar.stickCounter = countStickman
        print()
        for name in self.__class__.figureName:
            print(name)      
       
    def addFigure(self, name):
        if(self.__class__.figureName[0] == None):
            self.__class__.figureName[0] = name
        else:    
            self.__class__.figureName.insert(0, name)
       
    def removeFigure(self, index):
        if(len(self.__class__.figureName) > 1):
            lastIndex = len(self.__class__.figureName)-1
            self.__class__.figureName[index] = None
            self.__class__.figureName.remove(None)
        else:   
            self.__class__.figureName[0] = None       
   
    def removeAllFigure(self):   
        while(len(self.figureName) > 1):
            self.removeFigure(0)
        self.removeFigure(0)
                   
#class DrawingSpace(RelativeLayout):
#    def on_children(self, instance, value):
#        self.status_bar.counter = len(self.on_30_sep)

-----------------------------------------------------------------------------------------

# File name: comiccreator.py
import kivy
kivy.require('1.7.0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout

Builder.load_file('toolbox.kv')
Builder.load_file('drawingspace.kv')
Builder.load_file('comicwidgets.kv')
Builder.load_file('generaloptions.kv')
Builder.load_file('statusbar.kv')

class ComicCreator(AnchorLayout):
    pass

class ComicCreatorApp(App):
    def build(self):
        return ComicCreator()

if __name__=="__main__":
    ComicCreatorApp().run()

------------------------------------------------------------------------------------------

# File name: comicwidgets.kv
#:kivy 1.7.0
#:import comicwidgets comicwidgets
<DraggableWidget>:
    size_hint: None, None

<StickMan>:
    size: 48,48
    canvas:
        PushMatrix
        Line:
            circle: 24,38,5
        Line:
            points: 24,33,24,15
        Line:
            points: 14,5,24,15
        Line:
            points: 34,5,24,15
        Translate:
            y: 48-8
        Rotate:
            angle: 180
            axis: 1,0,0
        Line:
            points: 14,5,24,15
        Line:
            points: 34,5,24,15
        PopMatrix

--------------------------------------------------------------------------------------------

# File name: comicwidgets.py
import kivy
kivy.require('1.7.0')
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Line

class DraggableWidget(RelativeLayout):
    def __init__(self,  **kwargs):
        self.selected = None
        self.touched = False
        super(DraggableWidget, self).__init__(**kwargs)

    def on_touch_down(self, touch):
        if self.collide_point(touch.x, touch.y):
            self.touched = True
            self.select()
            return True
        return super(DraggableWidget, self).on_touch_down(touch)

    def select(self):
        if not self.selected:
            self.ix = self.center_x
            self.iy = self.center_y
            with self.canvas:
                self.selected = Line(rectangle=(0,0,self.width,self.height), dash_offset=2)
               
    def isSelect(self):
        return self.selected
                           
    def on_touch_move(self, touch):
        (x,y) = self.parent.to_parent(touch.x, touch.y)
        if self.selected and self.touched and self.parent.collide_point(x - self.width/2, y -self.height/2):
            go = self.parent.general_options
            go.translation=(touch.x-self.ix,touch.y-self.iy)
            return True
        return super(DraggableWidget, self).on_touch_move(touch)

    def translate(self, x, y):
        self.center_x = self.ix = self.ix + x
        self.center_y = self.iy = self.iy + y

    def on_touch_up(self, touch):
        self.touched = False
        if self.selected:
            if not self.parent.general_options.group_mode:
                self.unselect()
        return super(DraggableWidget, self).on_touch_up(touch)

    def unselect(self):
        if self.selected:
            self.canvas.remove(self.selected)
            self.selected = None

class StickMan(DraggableWidget):
    pass

-----------------------------------------------------------------------------------------------------------

# File name: drawingspace.kv
#:kivy 1.7.0
#:import drawingspace drawingspace
<DrawingSpace@RelativeLayout>:

-----------------------------------------------------------------------------------------------------------

# File name: drawingspace.py
import kivy
kivy.require('1.7.0')
from kivy.uix.relativelayout import RelativeLayout

class DrawingSpace(RelativeLayout):
    figureName = [None]
    def on_30_sep(self, instance, value):
        self.status_bar.counter = len(self.children)
        countStickman = 0
        for name in self.__class__.figureName:
            if(name == "Stickman"):
                countStickman += 1
        self.status_bar.stickCounter = countStickman
   
    def on_30_sep(self):
        self.status_bar.counter = len(self.children)   
        countStickman = 0
        for name in self.__class__.figureName:
            if(name == "Stickman"):
                countStickman += 1
        self.status_bar.stickCounter = countStickman
        print()
        for name in self.__class__.figureName:
            print(name)      
       
    def addFigure(self, name):
        if(self.__class__.figureName[0] == None):
            self.__class__.figureName[0] = name
        else:    
            self.__class__.figureName.insert(0, name)
       
    def removeFigure(self, index):
        if(len(self.__class__.figureName) > 1):
            lastIndex = len(self.__class__.figureName)-1
            self.__class__.figureName[index] = None
            self.__class__.figureName.remove(None)
        else:   
            self.__class__.figureName[0] = None       
   
    def removeAllFigure(self):   
        while(len(self.figureName) > 1):
            self.removeFigure(0)
        self.removeFigure(0)
                   
#class DrawingSpace(RelativeLayout):
#    def on_children(self, instance, value):
#        self.status_bar.counter = len(self.on_30_sep)

-------------------------------------------------------------------------------------------------------

# File name: generaloptions.kv
#:kivy 1.7.0
#:import generaloptions generaloptions
<GeneralOptions>:
    orientation: 'horizontal'
    padding: 2
    Button:
        text: 'Clear'
        on_press: root.clear(*args)
    Button:
        text: 'Remove'
        on_release: root.remove(*args)
    ToggleButton:
        text: 'Group'
        on_state: root.group(*args)
    Button:
        text: 'Color'
        on_press: root.color(*args)
    ToggleButton:
        text: 'Gestures'
        on_state: root.gestures(*args)

---------------------------------------------------------------------------------------------------------

# File name: generaloptions.py
import kivy
kivy.require('1.7.0')
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty, ListProperty

class GeneralOptions(BoxLayout):
    group_mode = False
    translation = ListProperty(None)

    def clear(self, instance):
        ds = self.drawing_space
        self.drawing_space.clear_widgets()
        ds.removeAllFigure()
        ds.on_30_sep()

    def remove(self, instance):
        ds = self.drawing_space
        if (len(ds.children) > 0 and self.group_mode == False):
            ds.remove_widget(ds.children[0])
            ds.removeFigure(0)
        elif self.group_mode == True:   
            index = 0
            for child in self.drawing_space.children:
                if child.isSelect() != None:
                    ds.remove_widget(ds.children[index])
                    ds.removeFigure(index)
                index += 1               
                #print(index)
        ds.on_30_sep()       

    def group(self, instance, value):
        if value == 'down':
            self.group_mode = True
        else:
            self.group_mode = False
            self.unselect_all()

    def color(self, instance):
        pass

    def gestures(self, instance, value):
        pass

    def unselect_all(self):
        for child in self.drawing_space.children:
            child.unselect()

    def on_translation(self,instance,value):
        for child in self.drawing_space.children:
            if child.selected:
                child.translate(*self.translation)

--------------------------------------------------------------------------------------------------------------

# File name: statusbar.kv
#:kivy 1.7.0
#:import statusbar statusbar
<StatusBar>:
    msg_label: _msg_label
    orientation: 'horizontal'
    Label:
        text: 'Total numbers of Figures: ' + str(root.counter)
    Label:
        id: _msg_label
        text: "Kivy started"
    Label:
        text: 'StickmanNumber: ' + str(root.stickCounter)   

-------------------------------------------------------------------------------------------------------------

# File name: statusbar.py
import kivy
kivy.require('1.7.0')
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty, ObjectProperty

class StatusBar(BoxLayout):
    counter = NumericProperty(0)
    stickCounter = NumericProperty(0)
    previous_counter = 0

    def on_counter(self, instance, value):
        #self.on_children(instance, value)
        if value == 0:
            self.msg_label.text = "Drawing space cleared"
        elif value - 1 == self.__class__.previous_counter:
            self.msg_label.text = "Widget added"
        elif value + 1 == StatusBar.previous_counter:
            self.msg_label.text = "Widget removed"
        self.__class__.previous_counter = value

-----------------------------------------------------------------------------------------------------------

# File name: toolbox.kv
#:kivy 1.7.0
#:import toolbox toolbox
<ToolButton>:
    size_hint: None,None
    size: 48,48
    group: 'tool'
    canvas:
        PushMatrix:
        Translate:
            xy: self.x,self.y
    canvas.after:
        PopMatrix:

<ToolBox@GridLayout>:
    cols: 2
    padding: 2
    tool_circle: _tool_circle
    tool_line: _tool_line
    tool_stickman: _tool_stickman
    ToolCircle:
        id:  _tool_circle
        canvas:
            Line:
                circle: 24,24,14
    ToolLine:
        id: _tool_line
        canvas:
            Line:
                points: 10,10,38,38
    ToolStickman:
        id: _tool_stickman
        StickMan:
            pos_hint: {'center_x':.5,'center_y':.5}

---------------------------------------------------------------------------------------------------------------

# File name: toolbox.py
import kivy
kivy.require('1.7.0')
import math
from kivy.uix.togglebutton import ToggleButton
from kivy.graphics import Line
from comicwidgets import StickMan, DraggableWidget

class ToolButton(ToggleButton):
    def on_touch_down(self, touch):
        ds = self.parent.drawing_space
        if self.state == 'down' and ds.collide_point(touch.x, touch.y):
            (x,y) = ds.to_widget(touch.x, touch.y)
            self.draw(ds, x, y)
            return True
        return super(ToolButton, self).on_touch_down(touch)

    def draw(self, ds, x, y):
        pass

class ToolStickman(ToolButton):
    def draw(self, ds, x, y):
        sm = StickMan(width=48, height=48)
        sm.center = (x,y)
        ds.add_widget(sm)
        ds.addFigure("Stickman")
        ds.on_30_sep()

class ToolFigure(ToolButton):
    def draw(self, ds, x, y):
        (self.ix, self.iy) = (x,y)
        with ds.canvas:
            self.figure=self.create_figure(x,y,x+1,y+1)
        ds.bind(on_touch_move=self.update_figure)
        ds.bind(on_touch_up=self.end_figure)

    def update_figure(self, ds, touch):
        if ds.collide_point(touch.x, touch.y):
            (x,y) = ds.to_widget(touch.x, touch.y)
            ds.canvas.remove(self.figure)
            with ds.canvas:
                self.figure = self.create_figure(self.ix, self.iy,x,y)

    def end_figure(self, ds, touch):
        ds.unbind(on_touch_move=self.update_figure)
        ds.unbind(on_touch_up=self.end_figure)
        ds.canvas.remove(self.figure)
        (fx,fy) = ds.to_widget(touch.x, touch.y)
        self.widgetize(ds,self.ix,self.iy,fx,fy)

    def widgetize(self,ds,ix,iy,fx,fy):
        widget = self.create_widget(ix,iy,fx,fy)
        (ix,iy) = widget.to_local(ix,iy,relative=True)
        (fx,fy) = widget.to_local(fx,fy,relative=True)
        widget.canvas.add(self.create_figure(ix,iy,fx,fy))
        ds.add_widget(widget)
        self.addFigureName()
        ds.on_30_sep()

    def create_figure(self,ix,iy,fx,fy):
        pass

    def create_widget(self,ix,iy,fx,fy):
        pass
       
    def addFigureName(self):
        pass   

class ToolLine(ToolFigure):
    def create_figure(self,ix,iy,fx,fy):
        return Line(points=[ix, iy, fx, fy])

    def create_widget(self,ix,iy,fx,fy):
        pos = (min(ix, fx), min(iy, fy))
        size = (abs(fx-ix), abs(fy-iy))
        return DraggableWidget(pos = pos, size = size)
       
    def addFigureName(self):
        ds = self.parent.drawing_space
        ds.addFigure("Line")   

class ToolCircle(ToolFigure):
    def create_figure(self,ix,iy,fx,fy):
        return Line(circle=[ix,iy,math.hypot(ix-fx,iy-fy)])

    def create_widget(self,ix,iy,fx,fy):
        r = math.hypot(ix-fx, iy-fy)
        pos = (ix-r, iy-r)
        size = (2*r, 2*r)
        return DraggableWidget(pos = pos, size = size)

    def addFigureName(self):
        ds = self.parent.drawing_space
        ds.addFigure("Circle")

----------------------------------------------------------------------------------------------------

วันศุกร์ที่ 7 ตุลาคม พ.ศ. 2559

Comic Creator (Test)


[5801012620071]
  -  เปลี่ยนชื่อ method จาก on_children เป็น on_30_sep แล้วแก้ไขโปรแกรมให้สามารถ รันได้ ปรกติ
  -  สารมารถ remove เป็นแบบ group โดยการ select แล้วกด remove ได้
  -  สามารถนับจำนวน Stick man ทั้งหมดภายใน DrawingSpace ได้

mercurial log :  
changeset :   5:8baa1a16dffc
tag :              tip
user :            Ruchapol <a5820071@gmail.com>
date :            Fri Oct 14 00:49:54 2016 +0700
summary :    fix bug clear widget

changeset :   4:3afa77f65dc0
tag :              tip
user :            Ruchapol <a5820071@gmail.com>
date :            Wed Oct 05 22:29:31 2016 +0700
summary :    Fix bug

changeset :   3:e29ab67d74b4
user :            Ruchapol <a5820071@gmail.com>
date :            Wed Oct 05 21:17:13 2016 +0700
summary :    All Done

changeset :   2:1a7605cd8cf7
user :            Ruchapol <a5820071@gmail.com>
date :            Fri Sep 30 12:01:27 2016 +0700
summary :    Stick Man Number

changeset :  1:3c236f47b607
user :           Ruchapol <a5820071@gmail.com>
date :           Fri Sep 30 11:32:30 2016 +0700
summary :   removeSelect

changeset :  0:a4707dc6b421
user :           Ruchapol <a5820071@gmail.com>
date :           Fri Sep 30 11:06:26 2016 +0700
summary :   No.2



link :

   https://Ruchapol@bitbucket.org/Ruchapol/commic2

Building Software W8


slide link :
   https://docs.google.com/presentation/d/1gf-ns4duo3u3p6BiifhiBpL3f1lVSdBpPNtsypGtKTA/edit?usp=sharing

ความคืบหน้า 
  -  สามารถ commit โปรแกรม comic creator ขึ้น bit bucket โดยผ่านโปรแกรม mercurial ได้
  -  โปรแกรม comic creator สามารถวาดรูป สี่เหลี่ยมมุมฉาก และสี่เหลี่ยจตุรัส ได้ 

Building Software W7.2


slide link :
    https://docs.google.com/presentation/d/1IjUPyx6mqPIjYifH61v7OlRbbJwyv5SuVWNLnGN9GGI/edit?usp=sharing

ความคืบหน้า
  -  ศึกษา widget even (ต่อ)
  -  โปรแกรม comic creator สามารถวาดรูปสี่เหลี่ยมได้ แต่มีบัคที่รูปพรีวิว
  -  Install & ทดลอง mercurial โดยให้เก็บข้อมูล ไว้ที่ bit bucket แต่ยังไม่สามารถ Push และ Commit ผ่าน mercurial ได้

Building Software W7


slide link :
   https://docs.google.com/presentation/d/1agvW6DX_wFqvrzM5wEZVlWzps5G2_q6KtNHhQVQXJTM/edit?usp=sharing

ความคืบหน้า
  -  ศึกษา code และการทำงานของ Widget Events
- ศึกษา หลักการทำงานของ โปรแกรม Pong

Building Software W6


slide link :
    https://docs.google.com/presentation/d/17hi5oL5BZ9YEoiZe7NZ9zg19Yuq6UtYAXYMhX67u9_Y/edit?usp=sharing

ความคืบหน้า
  - ทดลองและศึกษา canvas ต่างๆ ของ kivy 

Building Software W5.2


slide link :
    https://docs.google.com/presentation/d/1hCVtJfmI7kdvJP1iiiwgewuu8rd8NKFrCoE3y9_XeYo/edit?usp=sharing

ความคืบหน้า
  -  ศึกษาและทดลอง code layout ของ kivy

Building Software W5


slide link :
   https://docs.google.com/presentation/d/1BAC8ZDguYQxKdSG7SEYJ3IO_r_1dvudQRLfSmBm2QG0/edit?usp=sharing

ความคืบหน้า
  -  โปรแกรม Tic Tac Toe สามารถสั่งงาน method ข้าม class ได้โดยใช้วิธีการป้อน object ที่มาจากคลาสที่จะสั่งงานเข้าไปเป็น argument ที่จะนำไปเป็น constructure ของ class ที่สั่งงาน แทนวิธีการ inheritance
  - Install kivy & try kivy code

Building Software W4.2


slide link :
    https://docs.google.com/presentation/d/1jaecn0yGuiAHlI9kKVH1HTAJjIsDcQq7ocOEEmfLDfU/edit?usp=sharing

ความคืบหน้า
  -  สามารถเขียนโปรแกรม Tic Tac Toe แบบ class ให้ทำงานได้ โดยใช้หลักการ inheritance

Building Software W4



slide link :
    https://docs.google.com/presentation/d/1U-3OOqP2fTIflfkv4iWFm18o9IxSNuOsdzvKBEdvk3w/edit#slide=id.g1676696d39_0_75

ความคืบหน้า
  - Tic Tac Toe แบบธรรมดาสามารถใช้งานได้
  - เริ่มเขียนโปรแกรม Tic Tac Toe แบบ class แต่ติดปัญหาเรื่องการสั่งงานข้าม class
  - Install Eclipse & Python plug-ins