Just for fun: port of the Transit Bezier test

...for fun and to prove the python-efl power,
the test is half the size of the C version
This commit is contained in:
Davide Andreoli 2015-01-17 20:05:40 +01:00
parent 28d1e6853e
commit ea60044620
2 changed files with 163 additions and 0 deletions

View File

@ -123,6 +123,7 @@ items = [
("Transit Resizable", "test_transit", "transit7_clicked"),
("Transit Custom", "test_transit", "transit8_clicked"),
("Transit Chain", "test_transit", "transit9_clicked"),
("Transit Bezier", "test_transit_bezier", "transit_bezier_clicked"),
]),
("Entries", [
("Entry", "test_entry", "entry_clicked"),

View File

@ -0,0 +1,162 @@
#!/usr/bin/env python
# encoding: utf-8
import os
from efl.evas import Line
from efl import elementary
from efl.elementary.button import Button
from efl.elementary.window import Window, ELM_WIN_BASIC
from efl.elementary.background import Background
from efl.elementary.image import Image
from efl.elementary.label import Label
from efl.elementary.transit import Transit, ELM_TRANSIT_TWEEN_MODE_BEZIER_CURVE
script_path = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(script_path, "images")
SEGMENT_MAX = 200
BTN_SIZE = 50
WIN_W, WIN_H = 400, 400 + BTN_SIZE
CTRL_W, CTRL_H = 15, 15
ctrl_pt1 = None
ctrl_pt2 = None
label = None
bezier_lines = []
def clamp(minimum, x, maximum):
return max(minimum, min(x, maximum))
def v_get():
x, y, w, h = ctrl_pt1.geometry
v1 = float((x - (w/2))) / WIN_W;
v2 = 1 - float((y - (h/2))) / (WIN_H - BTN_SIZE)
x, y, w, h = ctrl_pt2.geometry
v3 = float((x - (w/2))) / WIN_W;
v4 = 1 - float((y - (h/2))) / (WIN_H - BTN_SIZE)
return (v1, v2, v3, v4)
def update_curve():
v1, v2, v3, v4 = v_get()
label.text = "<align=left><b>Control Points:</b></br>" \
"x1: %0.2f y1: %0.2f</br>" \
"x2: %0.2f y2: %0.2f</align>" % (v1, v2, v3, v4)
prev_x, prev_y = 0, WIN_H - BTN_SIZE - 1
for i, line in enumerate(bezier_lines):
progress = float(i) / float((SEGMENT_MAX - 1))
tx = (3 * progress * pow(1 - progress, 2) * v1) + \
(3 * pow(progress, 2) * (1 - progress) * v3) + \
(pow(progress, 3) * 1)
ty = (3 * progress * pow((1 - progress), 2) * v2) + \
(3 * pow(progress, 2) * (1 - progress) * v4) + \
(pow(progress, 3) * 1)
cur_x = int(tx * WIN_W)
cur_y = (WIN_H - BTN_SIZE - 1) - (int(ty * (WIN_H - BTN_SIZE)))
line.xy = prev_x, prev_y, cur_x, cur_y
prev_x, prev_y = cur_x, cur_y
def ctrl_pt_mouse_down_cb(obj, event):
obj.data["dragging"] = True
def ctrl_pt_mouse_up_cb(obj, event):
obj.data["dragging"] = False
def ctrl_pt_mouse_move_cb(obj, event):
if obj.data["dragging"]:
x = (event.position.canvas.x - (CTRL_W/2))
y = (event.position.canvas.y - (CTRL_H/2))
x = clamp(-(CTRL_W/2), x, WIN_W - (CTRL_W/2))
y = clamp(-(CTRL_H/2), y, WIN_H - (CTRL_H/2))
obj.move(x,y)
obj.data["line"].end = x + (CTRL_W/2), y + (CTRL_H/2)
update_curve()
def transit_del_cb(obj):
for w in ctrl_pt1, ctrl_pt2, ctrl_pt1.data["line"], ctrl_pt2.data["line"]:
w.show()
def btn_clicked_cb(btn):
v1, v2, v3, v4 = v_get()
transit = Transit(duration=1.0, auto_reverse=True,
tween_mode=ELM_TRANSIT_TWEEN_MODE_BEZIER_CURVE,
tween_mode_factor_n=[v1, v2, v3, v4])
transit.object_add(btn)
transit.effect_translation_add(0, 0, (WIN_W - BTN_SIZE), 0)
transit.del_cb_set(transit_del_cb)
transit.go()
for w in ctrl_pt1, ctrl_pt2, ctrl_pt1.data["line"], ctrl_pt2.data["line"]:
w.hide()
def transit_bezier_clicked(obj, item=None):
global ctrl_pt1, ctrl_pt2, label, bezier_lines
win = Window("transit_bezier", ELM_WIN_BASIC, title="Transit Bezier",
autodel=True, size=(WIN_W, WIN_H))
# BG. Fix window size
bg = Background(win, size_hint_min=(WIN_W, WIN_H))
win.resize_object_add(bg)
bg.show()
# Control Point 1
line = Line(win.evas, anti_alias=True, pass_events=True,
color=(0, 174, 219, 255),
xy=(0, WIN_W, 100 + (CTRL_W/2) , 300 + (CTRL_H/2)))
ctrl_pt1 = Image(win, file=os.path.join(img_path, "bubble.png"),
size=(CTRL_W,CTRL_H), pos=(100,300))
ctrl_pt1.on_mouse_down_add(ctrl_pt_mouse_down_cb)
ctrl_pt1.on_mouse_up_add(ctrl_pt_mouse_up_cb)
ctrl_pt1.on_mouse_move_add(ctrl_pt_mouse_move_cb)
ctrl_pt1.data["dragging"] = False
ctrl_pt1.data["line"] = line
ctrl_pt1.show()
line.show()
# Control Point 2
line = Line(win.evas, anti_alias=True, pass_events=True,
color=(219, 174, 0, 255),
xy=(WIN_W, 0, 300 + (CTRL_W/2), 100 + (CTRL_H/2)))
ctrl_pt2 = Image(win, file=os.path.join(img_path, "bubble.png"),
size=(CTRL_W,CTRL_H), pos=(300,100))
ctrl_pt2.on_mouse_down_add(ctrl_pt_mouse_down_cb)
ctrl_pt2.on_mouse_up_add(ctrl_pt_mouse_up_cb)
ctrl_pt2.on_mouse_move_add(ctrl_pt_mouse_move_cb)
ctrl_pt2.data["dragging"] = False
ctrl_pt2.data["line"] = line
ctrl_pt2.show()
line.show()
# Bezier Lines
for i in range(SEGMENT_MAX):
line = Line(win.evas, color=(255, 50, 50, 255), anti_alias=True,
pass_events=True)
bezier_lines.append(line)
line.show()
# Label
label = Label(win, size=(WIN_W, 50), pass_events=True)
label.show()
# Button
btn = Button(win, text="Go", size=(BTN_SIZE,BTN_SIZE),
pos=(0, WIN_H - BTN_SIZE))
btn.callback_clicked_add(btn_clicked_cb)
btn.show()
update_curve()
win.show()
if __name__ == "__main__":
elementary.init()
transit_bezier_clicked(None)
elementary.run()
elementary.shutdown()