aboutsummaryrefslogtreecommitdiffstats
path: root/examples/elementary/test_core_evas_smart.py
blob: a5422ad97ea523740062253232fac54d75197206 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# encoding: utf-8

import os
from random import randint

from efl.evas import SmartObject, Smart, EXPAND_BOTH, FILL_BOTH, Rectangle, \
    Line, FilledImage, Polygon, Text, SmartCbDescription
from efl import elementary
from efl.elementary.window import StandardWindow
from efl.elementary.box import Box
from efl.elementary.button import Button


script_path = os.path.dirname(os.path.abspath(__file__))
ic_file = os.path.join(script_path, "images", "logo.png")
objects = []


def random_color():
    return (randint(0, 255), randint(0, 255), randint(0, 255), 255)


class MySmart(Smart):

    @staticmethod
    def resize(smart_object, w, h):
        print("RESIZE", w, h)
        smart_object.bg.size = w, h
        smart_object.obj_text.size = w, 15
        smart_object.dragpos.size = w, 15
        smart_object.dragsize.size = 15, 15
        smart_object.dragsize.pos = smart_object.bg.pos[0] + w - 15, smart_object.bg.pos[1] + h - 15

    @staticmethod
    def move(smart_object, x, y):
        print("MOVE", x, y)
        smart_object.bg.pos = x, y
        smart_object.obj_text.pos = x, y
        smart_object.dragpos.pos = x, y
        smart_object.dragsize.pos = x + smart_object.bg.size[0] - 15, y + smart_object.bg.size[1] - 15
        smart_object.obj_rect.pos = x + 5, y + 20
        smart_object.obj_line.geometry = x + 30, y + 20, 15, 15
        smart_object.obj_image.pos = x + 30, y + 45

        smart_object.obj_poly.points_clear()
        smart_object.obj_poly.point_add(x + 5 + 0,  y + 45 + 0)
        smart_object.obj_poly.point_add(x + 5 + 15, y + 45 + 15)
        smart_object.obj_poly.point_add(x + 5 + 0,  y + 45 + 15)

    @staticmethod
    def delete(smart_object):
        print("my delete")

    @staticmethod
    def show(smart_object):
        print("my show")
        for o in smart_object:
            o.show()

    @staticmethod
    def hide(smart_object):
        print("my hide")
        for o in smart_object:
            o.hide()

    @staticmethod
    def clip_set(smart_object, o):
        pass

    @staticmethod
    def clip_unset(smart_object):
        pass

descriptions = (
    SmartCbDescription("mycb", "i"),
    )


class MySmartObj(SmartObject):

    def __init__(self, canvas, smart):
        SmartObject.__init__(self, canvas, smart)

        # gray background
        self.bg = Rectangle(canvas, color=(128, 128, 128, 128))
        self.member_add(self.bg)

        # green dragbar to move the obj
        self.dragpos = Rectangle(canvas, color=(0, 128, 0, 128))
        self.member_add(self.dragpos)
        self.dragpos.on_mouse_down_add(self.start_drag_move)
        self.dragpos.on_mouse_up_add(self.stop_drag_move)

        # blue rect to resize the obj
        self.dragsize = Rectangle(canvas, color=(0, 0, 128, 128))
        self.member_add(self.dragsize)
        self.dragsize.on_mouse_down_add(self.start_drag_resize)
        self.dragsize.on_mouse_up_add(self.stop_drag_resize)

        # testing factories
        self.obj_rect = Rectangle(canvas, size=(15, 15), color=random_color())
        self.member_add(self.obj_rect)
        self.obj_rect.on_mouse_down_add(lambda o, e: self.hide())
        self.obj_line = Line(canvas, color=random_color())
        self.member_add(self.obj_line)
        self.obj_image = FilledImage(canvas, file=ic_file, size=(20, 20))
        self.member_add(self.obj_image)
        self.obj_poly = Polygon(canvas, color=random_color())
        self.member_add(self.obj_poly)
        self.obj_text = Text(canvas, color=(0, 0, 0, 255), font="Sans",
                                  pass_events=True, text="Drag me")
        self.member_add(self.obj_text)

    # dragpos (move obj)
    def start_drag_move(self, obj, event):
        self.on_mouse_move_add(self.mouse_move_cb)

    def stop_drag_move(self, obj, event):
        self.on_mouse_move_del(self.mouse_move_cb)

    def mouse_move_cb(self, obj, event):
        x, y = event.position.canvas
        self.pos = x - self.bg.size[0] / 2, y

    # dragsize (resize obj)
    def start_drag_resize(self, obj, event):
        self.on_mouse_move_add(self.mouse_move_resize_cb)

    def stop_drag_resize(self, obj, event):
        self.on_mouse_move_del(self.mouse_move_resize_cb)

    def mouse_move_resize_cb(self, obj, event):
        x, y = event.position.canvas
        self.size = x - self.bg.pos[0], y - self.bg.pos[1]


def btn_add_cb(b):
    sm = MySmart(callback_descriptions=descriptions)
    print(sm.callback_descriptions)
    so = MySmartObj(b.evas, sm)
    so.size = 100, 100
    so.pos = 100, 100
    so.show()
    objects.append(so)


def btn_del_cb(b):
    objects.pop().delete()


def btn_hide_cb(b):
    for o in objects:
        o.hide()


def btn_show_cb(b):
    for o in objects:
        o.show()


def core_evas_smart_clicked(obj, item=None):
    win = StandardWindow("evassmart", "Evas Smart Object Test", autodel=True)
    print(win.callback_descriptions_get())
    if obj is None:
        win.callback_delete_request_add(lambda o: elementary.exit())

    box = Box(win, horizontal=True,
              size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
    box.show()
    win.resize_object_add(box)

    b = Button(win, text="Add one", size_hint_align=(0.0, 0.0))
    b.callback_clicked_add(btn_add_cb)
    box.pack_end(b)
    b.show()

    b = Button(win, text="Del last", size_hint_align=(0.0, 0.0))
    b.callback_clicked_add(btn_del_cb)
    box.pack_end(b)
    b.show()

    b = Button(win, text="Hide all", size_hint_align=(0.0, 0.0))
    b.callback_clicked_add(btn_hide_cb)
    box.pack_end(b)
    b.show()

    b = Button(win, text="Show all", size_hint_align=(0.0, 0.0))
    b.callback_clicked_add(btn_show_cb)
    box.pack_end(b)
    b.show()

    win.resize(320, 320)
    win.show()


if __name__ == "__main__":
    import logging
    efl_log = logging.getLogger("efl")
    efl_log_form = logging.Formatter(
        "[%(name)s] %(levelname)s in %(funcName)s:%(lineno)d - %(message)s"
        )
    efl_log_handler = logging.StreamHandler()
    efl_log_handler.setFormatter(efl_log_form)
    efl_log.addHandler(efl_log_handler)
    core_evas_smart_clicked(None)
    elementary.run()