-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
106 lines (75 loc) · 2.67 KB
/
main.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
#i Pass empty args on to Kivy, because it annoyingly parses them on import
import sys
argv = sys.argv[1:]
sys.argv = sys.argv[:1]
import os
from kivy.app import App
from kivy import platform
from kivy.core.window import Window
from kivy.properties import (ObjectProperty, StringProperty)
import sys
from os.path import abspath, join, dirname
nogo_path = dirname(abspath(__file__))
sys.path.append(nogo_path)
sys.path.append(dirname(nogo_path))
sys.path.append(join(nogo_path, 'ext'))
# if platform == 'android':
# from .gui import board
# else:
# from nogo2.gui import board
from gui import board, misc
from widgetcache import WidgetCache
import menu
print('cb', misc.ColouredButton)
print('board', board.__file__)
print('argv is', sys.argv)
class NogoApp(App):
cache = ObjectProperty(WidgetCache())
stone_type = StringProperty('simple')
def __init__(self, board_size=19, load_sgf=None, **kwargs):
super().__init__(**kwargs)
self.board_size = board_size
self.load_sgf = load_sgf
def build(self):
w = board.PhoneBoardView()
w.ids.bc.board.gridsize = self.board_size
if self.load_sgf is not None:
w.ids.bc.board.load_sgf_from_file('', [self.load_sgf])
Window.bind(on_keyboard=self.key_input)
Window.bind(on_request_close=self.on_request_close)
return w
def key_input(self, window, key, scancode, codepoint, modifier):
print('Received key {}'.format(key))
if key == 27:
self.back_button_leave_app()
return True # back button now does nothing on Android
return False
def back_button_leave_app(self):
if platform != 'android':
return
from jnius import autoclass
activity = autoclass('org.kivy.android.PythonActivity')
activity.moveTaskToBack(True)
def play_stone_sound(self):
pass
def on_pause(self):
# Stop LZ pondering to avoid causing problems in the background
self.root.ids.bc.board.lz_ponder(False)
return True
def on_stop(self):
self.clean_up_subprocess()
def on_request_close(self, *args, **kwargs):
self.clean_up_subprocess()
def clean_up_subprocess(self):
self.root.ids.bc.board.lz_ponder(False)
self.root.ids.bc.board.lz_wrapper.kill()
def run(*args, **kwargs):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--board-size', type=int, default=19)
parser.add_argument('--load-sgf', default=None)
args, unknown = parser.parse_known_args(argv)
NogoApp(board_size=args.board_size,
load_sgf=args.load_sgf).run()
if __name__ == "__main__":
run()