This repository has been archived on 2021-09-15. You can view files and clone it, but cannot push or open issues or pull requests.
M2IHMPalette/Palette/palette.py

168 lines
4.9 KiB
Python
Raw Normal View History

2017-01-19 13:43:08 +00:00
import turtle
2017-01-19 14:14:44 +00:00
from LibCommon import myIvy
2017-01-19 13:43:08 +00:00
from ivy.std_api import *
2017-01-22 22:00:45 +00:00
2017-01-19 13:43:08 +00:00
class Form:
2017-01-22 22:00:45 +00:00
def __init__(self, t, x, y, color):
self.t = t
2017-01-19 13:43:08 +00:00
self.x = x
self.y = y
2017-01-22 22:00:45 +00:00
self.color = color
self.color_default = color
def _setup_draw(self, x, y, color):
self.t.penup()
self.t.setposition(x, y)
self.t.pendown()
self.t.color(color, color)
self.t.begin_fill()
def _end_draw(self):
self.t.end_fill()
self.t.color(self.t.my_turtle_color)
self.t.penup()
self.t.home()
2017-01-19 13:43:08 +00:00
def is_in(self, x, y):
return x == self.x and y == self.y
2017-01-22 22:00:45 +00:00
def draw(self):
self._setup_draw(self.x, self.y, self.color)
self.t.dot()
self._end_draw()
def delete(self):
self.color = self.t.bgcolor
self.draw()
self.color = self.color_default
@staticmethod
def aim_delete(l_form, type_form, x, y, color=""):
for form in l_form:
2017-01-25 06:57:14 +00:00
if form.__class__.__name__.upper() == type_form.upper() and form.is_in(x, y) \
2017-01-26 21:29:32 +00:00
and color in ("None", form.color):
2017-01-22 22:00:45 +00:00
form.delete()
l_form.remove(form)
break
for form in l_form:
if form.is_in(x, y):
form.draw()
return l_form
@staticmethod
def aim_move(l_form, x, y, tx, ty, color=""):
for form in l_form:
if form.is_in(x, y)\
2017-01-26 21:29:32 +00:00
and color in ("None", form.color):
2017-01-22 22:00:45 +00:00
form.delete()
form.x = tx
form.y = ty
form.draw()
break
return l_form
2017-01-25 06:57:14 +00:00
class Rond(Form):
2017-01-22 22:00:45 +00:00
def __init__(self, t, x, y, radius=50, color="red"):
super().__init__(t, x, y, color)
2017-01-19 13:43:08 +00:00
self.radius = radius
def is_in(self, x, y):
2017-01-26 21:29:32 +00:00
return (x - self.x) ** 2 + (y - self.y - self.radius) ** 2 < self.radius ** 2
2017-01-19 13:43:08 +00:00
2017-01-22 22:00:45 +00:00
def draw(self):
self._setup_draw(self.x, self.y, self.color)
self.t.circle(self.radius)
self._end_draw()
2017-01-19 13:43:08 +00:00
class Rectangle(Form):
2017-01-22 22:00:45 +00:00
def __init__(self, t, x, y, vert=50, hor=80, color="red"):
super().__init__(t, x, y, color)
2017-01-19 13:43:08 +00:00
self.vert = vert
self.hor = hor
self.color = color
def is_in(self, x, y):
return 0 <= (x - self.x) <= self.hor and 0 <= (y - self.y) <= self.vert
2017-01-22 22:00:45 +00:00
def draw(self):
self._setup_draw(self.x, self.y, self.color)
self.t.setposition(self.x + self.hor, self.y)
self.t.setposition(self.x + self.hor, self.y + self.vert)
self.t.setposition(self.x, self.y + self.vert)
self.t.setposition(self.x, self.y)
self._end_draw()
2017-01-19 13:43:08 +00:00
class MyTurtle(turtle.Turtle):
2017-01-19 14:14:44 +00:00
def __init__(self, color="green", bgcolor="#FFFFFF"):
2017-01-19 13:43:08 +00:00
turtle.Turtle.__init__(self, shape="turtle")
2017-01-19 10:51:42 +00:00
2017-01-19 14:14:44 +00:00
self.bgcolor = bgcolor
2017-01-19 13:43:08 +00:00
self.pensize(3)
self.my_turtle_color = color
self.color(color)
self.screen = turtle.Screen()
2017-01-19 14:14:44 +00:00
self.screen.bgcolor(bgcolor)
2017-01-19 13:43:08 +00:00
self.screen.title("Palette")
self.screen.setup(1000, 600, 1, -1)
self.screen.setworldcoordinates(0, 600, 1000, 0)
self.screen.listen()
self.screen.onclick(self.onclick)
2017-01-19 14:14:44 +00:00
def onclick(self, x, y, **kwargs):
2017-01-19 13:43:08 +00:00
myturtle.goto(x, y)
2017-01-19 14:14:44 +00:00
IvySendMsg("PALETTE x={} y={}".format(x, y))
2017-01-19 13:43:08 +00:00
2017-01-26 21:29:32 +00:00
2017-01-19 13:43:08 +00:00
class MyIvyPalette(myIvy.MyIvy):
2017-01-22 22:00:45 +00:00
LIST_FORM = []
2017-01-19 13:43:08 +00:00
def _createbind(self):
2017-01-22 22:00:45 +00:00
IvyBindMsg(self.create, '^MULTIMODAL:creer forme=(.*) x=(.*) y=(.*) couleur=(.*)')
IvyBindMsg(self.move, '^MULTIMODAL:deplacer ca_x=(.*) ca_y=(.*) la_x=(.*) la_y=(.*) couleur=(.*)')
IvyBindMsg(self.delete, '^MULTIMODAL:supprimer forme=(.*) x=(.*) y=(.*) couleur=(.*)')
2017-01-19 13:43:08 +00:00
def create(self, agent, *larg):
2017-01-26 21:29:32 +00:00
print("create " + str(larg))
2017-01-19 13:43:08 +00:00
if larg[0] == "RECTANGLE":
2017-01-22 22:00:45 +00:00
r = Rectangle(myturtle, self.__my_int(larg[1]), self.__my_int(larg[2]), color=larg[3])
r.draw()
self.LIST_FORM.append(r)
2017-01-25 06:57:14 +00:00
elif larg[0] == "ROND":
c = Rond(myturtle, self.__my_int(larg[1]), self.__my_int(larg[2]), color=larg[3])
2017-01-22 22:00:45 +00:00
c.draw()
self.LIST_FORM.append(c)
def move(self, agent, *larg):
2017-01-26 21:29:32 +00:00
print("move " + str(larg))
self.LIST_FORM = Form.aim_move(self.LIST_FORM, self.__my_int(larg[0]),
2017-01-25 06:57:14 +00:00
self.__my_int(larg[1]), self.__my_int(larg[2]),
self.__my_int(larg[3]), color=larg[4])
2017-01-22 22:00:45 +00:00
def delete(self, agent, *larg):
2017-01-26 21:29:32 +00:00
print("delete " + str(larg))
2017-01-25 06:57:14 +00:00
self.LIST_FORM = Form.aim_delete(self.LIST_FORM, larg[0],
self.__my_int(larg[1]), self.__my_int(larg[2]), color=larg[3])
2017-01-19 10:51:42 +00:00
def __my_int(self, str):
2017-01-22 22:00:45 +00:00
return int(str.split(".")[0])
2017-01-19 13:43:08 +00:00
myturtle = MyTurtle()
2017-01-19 10:51:42 +00:00
myturtle.penup()
myturtle.color("green")
2017-01-19 13:43:08 +00:00
2017-01-19 14:14:44 +00:00
my_ivy = MyIvyPalette("Palette", "127.255.255.255:2010")
2017-01-19 13:43:08 +00:00
myturtle.screen._root.mainloop()
2017-01-22 22:00:45 +00:00