File: select.py

package info (click to toggle)
glitch 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 488 kB
  • ctags: 649
  • sloc: python: 3,432; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 2,171 bytes parent folder | download | duplicates (2)
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

from __future__ import with_statement

import gtk

import glitch, glitch.gtk
from glitch.selectable import Selectable
from glitch.limbo.lights import LightSwitch, AmbientLight, DiffuseLight
from glitch.limbo.material import Material
from glitch.limbo.objects import Cube, Sphere, Teapot

def has_descendent(x, y):
    if y in x.children:
        return True
    else:
        for child in x.children:
            if has_descendent(child, y):
                return True

    return False

def on_click(window, event, camera):
    global selected
    (x, y) = event.get_coords()
    old_selected = selected
    selected = None

    # XXX: This might cause a buffer swap that's redundant.

    with camera as (w, h):
        hits = camera.select(camera.context, x, (h - y))

        if hits:
            for o in [teapot, cube, sphere]:
                if has_descendent(o, hits[-1]):
                    selected = o

        if selected != old_selected:
            if old_selected is not None:
                (old_selected.r, old_selected.g, old_selected.b) = \
                    (0.2, 0.2, 0.3)

            if selected is not None:
                (selected.r, selected.g, selected.b) = (0.2, 0.2, 1.0)

            camera.refresh_now()

if __name__=='__main__':
    selected = None
    teapot = Material(0.2, 0.2, 0.3, children=[
        glitch.Translate(-2, children=[
            Selectable(children=[Teapot(size=0.5)])])])
    cube = Material(0.2, 0.2, 0.3, children=[
        glitch.Translate(x=2, children=[
            Selectable(children=[Cube()])])])
    sphere = Material(0.2, 0.2, 0.3, children=[
        glitch.Translate(z=-2, children=[
            Selectable(children=[Sphere()])])])
    camera = glitch.gtk.GtkCamera(eye=[0, 1, 3], children=[
        LightSwitch(children=[
            AmbientLight(intensity=0.3, children=[
            DiffuseLight(intensity=0.4, children=
                [teapot, cube, sphere])])])])

    w = camera.make_window()
    w.show_all()

    camera.add_events(
        gtk.gdk.POINTER_MOTION_MASK |
        gtk.gdk.POINTER_MOTION_HINT_MASK)
    camera.connect('motion-notify-event', on_click, camera)

    gtk.main()