File: clock_linear_diff.py

package info (click to toggle)
pysiogame 4.20.01-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,876 kB
  • sloc: python: 48,742; xml: 3,813; sh: 30; makefile: 11
file content (318 lines) | stat: -rw-r--r-- 14,377 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding: utf-8 -*-

import pygame
import copy

import classes.extras as ex


class Clock:
    def __init__(self, game_board, wrapper, width, height, times, prefs):
        self.game_board = game_board
        self.show_outer_ring = prefs[0]
        self.show_minutes = prefs[1]
        self.show_24h = prefs[2]
        self.show_only_quarters_h = prefs[3]
        self.show_only_quarters_m = prefs[4]
        self.show_only_fives_m = prefs[5]
        self.show_roman = prefs[6]
        self.show_highlight = prefs[7]
        self.show_hour_offset = prefs[8]
        self.show_result = prefs[9]

        self.roman = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"]

        if self.game_board.mainloop.scheme is not None:
            color1 = self.game_board.mainloop.scheme.color1  # bright side of short hand
            color3 = self.game_board.mainloop.scheme.color3  # inner font color
            color5 = self.game_board.mainloop.scheme.color5  # dark side of short hand
            color7 = self.game_board.mainloop.scheme.color7  # inner circle filling

            color2 = self.game_board.mainloop.scheme.color2  # bright side of long hand
            color4 = self.game_board.mainloop.scheme.color4  # ex.hsv_to_rgb(170,255,255)#outer font color
            color6 = self.game_board.mainloop.scheme.color6  # dark side of long hand
            color8 = self.game_board.mainloop.scheme.color8  # outer circle filling
        else:
            color1 = ex.hsv_to_rgb(225, 70, 230)
            color3 = ex.hsv_to_rgb(225, 255, 255)
            color5 = ex.hsv_to_rgb(225, 180, 240)
            color7 = ex.hsv_to_rgb(225, 10, 255)

            color2 = ex.hsv_to_rgb(170, 70, 230)
            color4 = ex.hsv_to_rgb(170, 255, 255)
            color6 = ex.hsv_to_rgb(170, 180, 240)
            color8 = ex.hsv_to_rgb(170, 10, 255)

        self.diff_h_col = ex.hsv_to_rgb(225, 150, 200)
        self.diff_hr_col = ex.hsv_to_rgb(225, 100, 210)
        self.diff_m_col = ex.hsv_to_rgb(170, 150, 200)
        self.diff_h_bcol = ex.hsv_to_rgb(225, 150, 170)
        self.diff_m_bcol = ex.hsv_to_rgb(170, 150, 170)
        self.white = (255, 255, 255)
        self.black = (0, 0, 0)

        #diff colours
        self.colors = [color1, color2]
        self.colors2 = [color3, color4]
        self.colors3 = [color5, color6]
        self.colors4 = [color7, color8]

        self.hand_coords = [[], []]
        self.size = [width, height]
        self.times = times

        self.clock_wrapper = wrapper  # self.board.ships[-1]
        self.clock_wrapper.font = game_board.clock_fonts[0]
        self.clock_wrapper.font2 = game_board.clock_fonts[1]
        self.clock_wrapper.font3 = game_board.clock_fonts[2]

        self.clock_wrapper.hidden_value = [2, 3]
        self.clock_wrapper.font_color = color2

        self.margin = 30 * self.size[0] / 500.0
        self.h_marker_len = 15 * self.size[1] / 150.0
        self.h_marker_len15 = 7 * self.size[1] / 150.0
        self.m_marker_len15 = 20 * self.size[1] / 150.0
        self.m_marker_len5 = 15 * self.size[1] / 150.0
        self.m_marker_len = 10 * self.size[1] / 150.0
        self.centre_margin = 35 * self.size[1] / 150.0
        self.diff_h = 25 * self.size[1] / 150.0

        self.draw_all(times)

    def draw_all(self, times):
        self.times = times
        self.diff_solution = self.get_diff()
        self.canvas = pygame.Surface([self.size[0], self.size[1] - 1])
        if self.game_board.mainloop.scheme is not None:
            self.canvas.fill(self.game_board.mainloop.scheme.u_color)
        else:
            self.canvas.fill((255, 255, 255))
        self.draw_linear_clock()
        self.clock_wrapper.painting = self.canvas.copy()

    def get_diff(self):
        t = copy.deepcopy(self.times)

        # calculate minutes
        if t[1][1] >= t[0][1]:
            m = t[1][1] - t[0][1]
        else:
            m = 60 + t[1][1] - t[0][1]
            t[1][0] -= 1

        # calculate hours
        if t[1][0] >= t[0][0]:
            h = t[1][0] - t[0][0]
        else:
            if self.show_24h:
                h = 24 + t[1][0] - t[0][0]
            else:
                h = 12 + t[1][0] - t[0][0]
        return [h, m]

    def draw_linear_clock(self):
        if self.show_24h:
            wrap = 24
        else:
            wrap = 12

        # scale the view to only the hours that need to be visible - not 1- 24, but time[0][0] - time[1][0] + 1
        line_length = self.size[0] - 2.0 * self.margin

        # draw markers and numbers for all hours displayed
        if self.times[0][0] != self.times[1][0]:
            if self.times[1][0] < self.times[0][0]:
                dist = self.times[1][0] + wrap - self.times[0][0]
            else:
                dist = self.times[1][0] - self.times[0][0]
        else:
            if self.times[1][1] < self.times[0][1]:
                dist = wrap
            else:
                dist = 0
        if dist > 0 and self.times[0] != self.times[1] and self.times[1][1] == 0:
            dist -= 1
        h_step = line_length * 1.0 / (dist + 1)
        hours = []

        for i in range(0, dist+2):
            pygame.draw.line(self.canvas, self.colors2[0],
                             [self.margin + i * h_step, self.size[1]//2 - self.centre_margin],
                             [self.margin + i * h_step, self.size[1]//2 - self.centre_margin - self.h_marker_len], 1)
            if (self.times[0][0] + i) % wrap != 0:
                val = str((self.times[0][0] + i) % wrap)
                hours.append((self.times[0][0] + i) % wrap)
            else:
                val = str(self.times[0][0] + i % wrap)
                hours.append(self.times[0][0] + i % wrap)
            font_size = self.clock_wrapper.font3.size(val)
            text = self.clock_wrapper.font3.render(val, 1, self.colors2[0])
            x = self.margin + i * h_step - int(font_size[0] / 2.0)
            y = self.size[1] // 2 - self.centre_margin - self.h_marker_len - font_size[1]
            self.canvas.blit(text, (x, y))

        # draw markers every 15 minutes
        for i in range(0, (dist+1) * 4):
            if i % 4 != 0:
                pygame.draw.line(self.canvas, self.colors2[0],
                                 [self.margin + i * h_step / 4.0, self.size[1] // 2 - self.centre_margin],
                                 [self.margin + i * h_step / 4.0, self.size[1] // 2 - self.centre_margin - self.h_marker_len15], 1)

        if self.times[0] != self.times[1]:
            # draw hour difference
            l = self.margin + self.times[0][1] * h_step / 60.0
            if self.times[1][1] == 0:
                r = self.size[0] - self.margin
            else:
                r = self.size[0] - (self.margin + (60 - self.times[1][1]) * h_step / 60.0)
            t = self.size[1]//2 - self.centre_margin + 1
            b = self.size[1]//2 - self.centre_margin + self.diff_h

            p = [[l, t], [r, t], [r, b], [l, b], [l, t]]

            pygame.draw.polygon(self.canvas, self.diff_h_col, p, 0)
            pygame.draw.polygon(self.canvas, self.diff_h_bcol, p, 1)

            # recolour the remainder of last hour
            if self.times[0][1] != self.times[1][1]:
                if self.times[0][1] < self.times[1][1] or self.times[1][1] == 0:
                    d = dist
                else:
                    d = (dist - 1)
                l2 = l + d * h_step
                p = [[l2, t], [r, t], [r, b], [l2, b], [l2, t]]
                pygame.draw.polygon(self.canvas, self.diff_hr_col, p, 0)
                pygame.draw.polygon(self.canvas, self.diff_h_bcol, p, 1)

                if self.show_result:
                    #display number of minutes inside remainder
                    val = "%sm" % self.diff_solution[1]
                    font_size = self.clock_wrapper.font3.size(val)
                    x = l2 + (r - l2) / 2 - font_size[0] / 2
                    y = self.size[1] // 2 - self.centre_margin + (self.diff_h - font_size[1]) / 2
                    if font_size[0] < (r - l2) - 5:
                        x = l2 + (r - l2) / 2 - font_size[0] / 2
                        text = self.clock_wrapper.font3.render(val, 1, self.white)
                    else:
                        x = r + 5
                        text = self.clock_wrapper.font3.render(val, 1, self.colors2[1])
                    self.canvas.blit(text, (x, y))

            # draw white lines - divide hours
            if dist > 0:
                for i in range(1, dist + 1):
                    if l + i * h_step < r:
                        pygame.draw.line(self.canvas, self.white, [l + i * h_step, t], [l + i * h_step, b], 1)

            if self.show_result:
                d = self.diff_solution[0]
                for i in range(0, d):
                    if i < d:
                        l = self.margin + h_step * i + self.times[0][1] * h_step / 60.0
                        r = self.margin + h_step * (i + 1) + self.times[0][1] * h_step / 60.0

                        val = "%sh" % (i + 1)
                        font_size = self.clock_wrapper.font3.size(val)
                        x = l + (r - l) / 2 - font_size[0] / 2
                        y = self.size[1] // 2 - self.centre_margin + (self.diff_h - font_size[1]) / 2
                        text = self.clock_wrapper.font3.render(val, 1, self.white)
                        self.canvas.blit(text, (x, y))

        # draw markers and numbers for all minutes
        m_step = line_length * 1.0 / 60
        for i in range(0, 61):
            if i % 15 == 0:
                m_len = self.m_marker_len15
            elif i % 5 == 0:
                m_len = self.m_marker_len5
            else:
                m_len = self.m_marker_len

            if i % 5 == 0:
                font_size = self.clock_wrapper.font3.size(str(i))
                text = self.clock_wrapper.font3.render(str(i), 1, self.colors2[1])
                x = self.margin + i * m_step - int(font_size[0] / 2.0)
                y = self.size[1] // 2 + self.centre_margin + self.m_marker_len15
                self.canvas.blit(text, (x, y))

            pygame.draw.line(self.canvas, self.colors2[1],
                             [self.margin + i * m_step, self.size[1]//2 + self.centre_margin],
                             [self.margin + i * m_step, self.size[1]//2 + self.centre_margin + m_len], 1)

        # draw minutes difference
        if self.times[0][1] < self.times[1][1]: #if first time is smaller than second time - all minutes in one hour
            l = self.margin + self.times[0][1] * m_step
            r = self.margin + self.times[1][1] * m_step
            t = self.size[1] // 2 + self.centre_margin - 1
            b = self.size[1] // 2 + self.centre_margin - self.diff_h

            p = [[l, t], [r, t], [r, b], [l, b], [l, t]]

            pygame.draw.polygon(self.canvas, self.diff_m_col, p, 0)
            pygame.draw.polygon(self.canvas, self.diff_m_bcol, p, 1)

            if self.show_result:
                val = "%sm" % (self.times[1][1] - self.times[0][1])
                font_size = self.clock_wrapper.font3.size(val)
                y = self.size[1] // 2 + self.centre_margin - self.diff_h + (self.diff_h - font_size[1]) / 2
                if font_size[0] < (r - l) - 5:
                    x = l + (r - l) / 2 - font_size[0] / 2
                    text = self.clock_wrapper.font3.render(val, 1, self.white)
                else:
                    x = r + 5
                    text = self.clock_wrapper.font3.render(val, 1, self.colors2[1])
                self.canvas.blit(text, (x, y))

        elif self.times[0][1] > self.times[1][1]:
            t = self.size[1] // 2 + self.centre_margin - 1
            b = self.size[1] // 2 + self.centre_margin - self.diff_h

            l1 = self.margin + self.times[0][1] * m_step
            r1 = self.size[0] - self.margin
            p1 = [[l1, t], [r1, t], [r1, b], [l1, b], [l1, t]]
            pygame.draw.polygon(self.canvas, self.diff_m_col, p1, 0)
            pygame.draw.polygon(self.canvas, self.diff_m_bcol, p1, 1)

            if self.show_result:
                val = "%sm" % (60 - self.times[0][1])
                font_size = self.clock_wrapper.font3.size(val)
                y = self.size[1] // 2 + self.centre_margin - self.diff_h + (self.diff_h - font_size[1]) / 2
                if font_size[0] < (r1 - l1) - 5:
                    x = l1 + (r1 - l1) / 2 - font_size[0] / 2
                    text = self.clock_wrapper.font3.render(val, 1, self.white)
                else:
                    x = r1 + 5
                    text = self.clock_wrapper.font3.render(val, 1, self.colors2[1])
                self.canvas.blit(text, (x, y))

            if self.times[1][1] != 0:
                l2 = self.margin
                r2 = self.margin + self.times[1][1] * m_step
                p2 = [[l2, t], [r2, t], [r2, b], [l2, b], [l2, t]]
                pygame.draw.polygon(self.canvas, self.diff_m_col, p2, 0)
                pygame.draw.polygon(self.canvas, self.diff_m_bcol, p2, 1)
                if self.show_result:
                    val = "%sm" % self.times[1][1]
                    font_size = self.clock_wrapper.font3.size(val)
                    y = self.size[1] // 2 + self.centre_margin - self.diff_h + (self.diff_h - font_size[1]) / 2
                    if font_size[0] < (r2 - l2) - 5:
                        x = l2 + (r2 - l2) / 2 - font_size[0] / 2
                        text = self.clock_wrapper.font3.render(val, 1, self.white)
                    else:
                        x = l2 - font_size[0] - 5
                        text = self.clock_wrapper.font3.render(val, 1, self.colors2[1])
                    self.canvas.blit(text, (x, y))

        # draw a horizontal lines through the centre of the view
        pygame.draw.line(self.canvas, self.colors2[0],
                         [self.margin, self.size[1]//2 - self.centre_margin],
                         [self.size[0]-self.margin, self.size[1]//2 - self.centre_margin], 2)

        pygame.draw.line(self.canvas, self.colors2[1],
                         [self.margin, self.size[1]//2 + self.centre_margin],
                         [self.size[0]-self.margin, self.size[1]//2 + self.centre_margin], 2)

        self.clock_wrapper.update_me = True