-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
executable file
·139 lines (122 loc) · 4.88 KB
/
visualize.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
import gi
from game import Game
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GdkPixbuf
import cairo
class RedBotPlayer(Gtk.Grid):
def __init__(self, file):
super(RedBotPlayer, self).__init__()
dwarf = GdkPixbuf.Pixbuf.new_from_file_at_scale("sprites/dwarf_%s.svg" % file, 50, 70, True)
potion = GdkPixbuf.Pixbuf.new_from_file_at_scale("sprites/potion.svg", 30, 30, True)
stone = GdkPixbuf.Pixbuf.new_from_file_at_scale("sprites/stone.svg", 30, 30, True)
self.potion_count = Gtk.Label()
self.stone_count = Gtk.Label()
self.attach(Gtk.Image().new_from_pixbuf(dwarf), 0, 0, 1, 2)
self.attach(Gtk.Image().new_from_pixbuf(potion), 1, 0, 1, 1)
self.attach(self.potion_count, 2, 0, 1, 1)
self.attach(Gtk.Image().new_from_pixbuf(stone), 1, 1, 1, 1)
self.attach(self.stone_count, 2, 1, 1, 1)
class RedBotPlayfield(Gtk.Grid):
def __init__(self, colors):
super(RedBotPlayfield, self).__init__()
self.colors = [Gdk.color_parse(c) for c in colors]
self.colors.append(Gdk.color_parse("black"))
self.set_row_homogeneous(True)
self.set_column_homogeneous(True)
self.modify_bg(Gtk.StateType.NORMAL, self.colors[-1])
def setup(self, plan):
def label(c, i):
label = Gtk.Label(c)
label.modify_fg(Gtk.StateType.NORMAL, self.colors[i])
return label
def parse_field(field):
PATHS = ['|', '/', '-', '\\', '#']
grid = Gtk.Grid()
grid.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("white"))
grid.set_border_width(1)
i = 0
for k, v in field.iteritems():
if k in PATHS:
grid.attach(label(k, int(v)), 0, i, 1, 1)
else:
grid.attach(label("%s : %s" %(k, v), -1), 0, i, 1, 1)
i = i + 1
return grid
for x in range(plan.columns_count):
for y in range(plan.rows_count):
# TODO: use some graphic for fields
element = self.get_child_at(x, plan.rows_count - y -1)
if element != None:
element.destroy()
self.attach(parse_field(plan[x][y]), x, plan.rows_count - y -1, 1, 1)
self.show_all()
# TODO: Create video from screenshots - see https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images
class RedBotVisualizer(Gtk.Window):
def __init__(self, directory):
Gtk.Window.__init__(self, title="Red Bot 2015")
dwarfs = ["blue", "green", "red", "violet"]
grid = Gtk.Grid()
self.players = []
self.playfield = RedBotPlayfield(dwarfs)
self.current_round = 0
self.round_label = Gtk.Label("round: " + str(self.current_round))
self.next_round_button = Gtk.Button(label="Next Round")
self.next_round_button.connect("clicked", self.on_next_button_clicked)
self.prev_round_button = Gtk.Button(label="Previous Round")
self.prev_round_button.connect("clicked", self.on_prev_button_clicked)
self.connect("delete-event", Gtk.main_quit)
grid.attach(self.prev_round_button, 0, 0, 1, 1)
grid.attach(self.next_round_button, 1, 0, 1, 1)
grid.attach(self.playfield, 0, 1, 2, 4)
grid.attach(self.round_label, 2, 0, 1, 1)
self.add(grid)
self.files = filter(lambda l: l.startswith("playfield-") and l.endswith(".txt"), os.listdir(directory))
self.file_index = 0
if self.files != []:
self.files.sort()
g = Game(self.files[self.file_index], [])
for i in range(g._get_strat_cnt()):
self.players.append(RedBotPlayer(dwarfs[i]))
grid.attach(self.players[i], 2, i + 1, 1, 1)
self.set_round(g)
self.show_all()
else:
print "no playfield found"
exit(1)
def make_screenshot(self):
gdk_win = self.get_window()
width = gdk_win.get_width()
height = gdk_win.get_height()
ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(ims)
Gdk.cairo_set_source_window(cr, gdk_win, 0, 0)
cr.paint()
ims.write_to_png("screenshot-%.4d.png" % self.current_round)
def set_round(self, game):
self.current_round = game._get_round()
self.round_label.set_text("round: " + str(self.current_round))
self.playfield.setup(game._get_plan())
for i in range(game._get_strat_cnt()):
potions, stones, _, _ = game._get_strat_data(i).split(",")
self.players[i].potion_count.set_text(potions)
self.players[i].stone_count.set_text(stones)
return game
def on_next_button_clicked(self, widget):
self.make_screenshot()
if self.file_index + 1 < len(self.files):
self.file_index += 1
self.set_round(Game(self.files[self.file_index], []))
def on_prev_button_clicked(self, widget):
self.make_screenshot()
if self.file_index > 0:
self.file_index -= 1
self.set_round(Game(self.files[self.file_index], []))
# todo add help
if __name__ == '__main__':
directory = sys.argv[1] if len(sys.argv) > 1 else "./"
win = RedBotVisualizer(directory)
Gtk.main()