Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/window.py~
blob: bad4fef71844707ca2224b581b633ecc95a0beac (plain)
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
# -*- coding: utf-8 -*-
#Copyright (c) 2009, Walter Bender

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

from constants import *
import pygtk
pygtk.require('2.0')
import gtk
from gettext import gettext as _
import math

try:
    from sugar.graphics import style
    GRID_CELL_SIZE = style.GRID_CELL_SIZE
except:
    GRID_CELL_SIZE = 0

from sprite_factory import *

class srWindow: pass

#
# handle launch from both within and without of Sugar environment 
#
def new_window(canvas, path, parent=None):

    

    # store class variables here
    tw = srWindow()
    tw.path = path
    tw.activity = parent

    # starting from command line
    # we have to do all the work that was done in CardSortActivity.py
    if parent is None:
        tw.sugar = False
        tw.canvas = canvas

    # starting from Sugar
    else:
        tw.sugar = True
        tw.canvas = canvas
        parent.show_all()

    tw.canvas.set_flags(gtk.CAN_FOCUS)
    tw.canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK)
    tw.canvas.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
    tw.canvas.add_events(gtk.gdk.POINTER_MOTION_MASK)
    tw.canvas.connect("expose-event", _expose_cb, tw)
    tw.canvas.connect("button-press-event", _button_press_cb, tw)
    tw.canvas.connect("button-release-event", _button_release_cb, tw)
    tw.canvas.connect("motion-notify-event", _mouse_move_cb, tw)
    tw.width = gtk.gdk.screen_width()
    tw.height = gtk.gdk.screen_height()-GRID_CELL_SIZE
    tw.area = tw.canvas.window
    tw.gc = tw.area.new_gc()
    tw.cm = tw.gc.get_colormap()
    tw.msgcolor = tw.cm.alloc_color('black')
    tw.sprites = []
    tw.scale = 1

    # Open the sliders
    y = 50
    tw.cards = [Sprite(tw,"CartaCristo",100,100,100,198,False),\
                Sprite(tw,"CartaGranMuralla",250,100,100,198,False),\
                Sprite(tw,"CartaMacchu",400,100,100,198,False)]
    tw.numbers = [Sprite(tw,"Carta1",100,100,100,198,False),\
                Sprite(tw,"Carta2",250,100,100,198,False),\
                Sprite(tw,"Carta3",400,100,100,198,False)]


    tw.cards[0].draw_slider_bottom()
    tw.cards[1].draw_slider_bottom()
    tw.cards[2].draw_slider_bottom()
    tw.numbers[0].draw_slider_top()
    tw.numbers[1].draw_slider_top()
    tw.numbers[2].draw_slider_top()


    # Start calculating
    tw.press = None
    tw.dragpos = 0,0

    return tw

#
# Button press
#
def _button_press_cb(win, event, tw):
    win.grab_focus()
    x, y = map(int, event.get_coords())
    tw.dragpos = x,y
    spr = findsprite(tw,(x,y))
    tw.press = spr
    return True

#
# Mouse move
#
def _mouse_move_cb(win, event, tw):
    if tw.press is None:
        tw.dragpos = 0,0
        return True

    win.grab_focus()
    x, y = map(int, event.get_coords())
    dx = x-tw.dragpos[0]
    if tw.press == tw.cards[2].spr or tw.press == tw.numbers[2].spr:
        # everything moves
        move(tw.cards[0].spr,(tw.cards[0].spr.x+dx,tw.cards[0].spr.y))
        move(tw.cards[1].spr,(tw.cards[1].spr.x+dx,tw.cards[1].spr.y))
        move(tw.cards[2].spr,(tw.cards[2].spr.x+dx,tw.cards[2].spr.y))
        move(tw.numbers[0].spr,(tw.numbers[0].spr.x+dx,tw.numbers[0].spr.y))
        move(tw.numbers[1].spr,(tw.numbers[1].spr.x+dx,tw.numbers[1].spr.y))
        move(tw.numbers[2].spr,(tw.numbers[2].spr.x+dx,tw.numbers[2].spr.y))
    # reset drag position
    tw.dragpos = x,y

#
# Button release
#
def _button_release_cb(win, event, tw):
    print tw.press
    if tw.press == None:
        return True
    if tw.press == tw.cards[1].spr:
        x = tw.cards[2].spr.x
        move(tw.cards[2].spr,(tw.cards[1].spr.x,tw.cards[2].spr.y))
        move(tw.cards[1].spr,(x,tw.cards[1].spr.y))
    elif tw.press == tw.cards[0].spr:
        tw.cards[0].draw_slider_bottom()
        tw.cards[1].draw_slider_bottom()
        tw.cards[2].draw_slider_bottom()
        tw.numbers[0].draw_slider_top()
        tw.numbers[1].draw_slider_top()
        tw.numbers[2].draw_slider_top()
    elif tw.press == tw.numbers[1].spr:
        x = tw.numbers[2].spr.x
        move(tw.numbers[2].spr,(tw.numbers[1].spr.x,tw.numbers[2].spr.y))
        move(tw.numbers[1].spr,(x,tw.numbers[1].spr.y))
    elif tw.press == tw.numbers[0].spr:
        tw.cards[0].draw_slider_top()
        tw.cards[1].draw_slider_top()
        tw.cards[2].draw_slider_top()
        tw.numbers[0].draw_slider_bottom()
        tw.numbers[1].draw_slider_bottom()
        tw.numbers[2].draw_slider_bottom()
    tw.press = None
    update_label(tw)

def update_label(tw):
    # calculate the values for D, C, and D*C (under the redicule)
    tw.activity.results_label.set_text( "my label changed " + str(tw.dragpos))
    tw.activity.results_label.show()
    return True

def _calc_C(tw):
    return "foo"
def _calc_D(tw):
    return "bar"
def _calc_DC(tw):
    return "usmp"

def _expose_cb(win, event, tw):
    redrawsprites(tw)
    return True

def _destroy_cb(win, event, tw):
    gtk.main_quit()