Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python gui #76

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add config file and improve gui
martinRenou committed May 19, 2018

Verified

This commit was signed with the committer’s verified signature.
tna-da-bot tna-da-bot
commit 41836cb1b73df2bf8020c38c025717ef4cd345c7
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,3 +2,4 @@ build/*
*.gch
src/get_share_path.cxx
gui/toonchess/get_share_path.py
gui/toonchess/__pycache__/*
106 changes: 95 additions & 11 deletions gui/toonchess/ToonChess.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
from traits.api import Button, Color, HasStrictTraits, Str, Range, Enum
from traitsui.api import Item, UItem, RangeEditor, spring, VGroup, HGroup, View
import subprocess
from concurrent import futures

from traits.api import (
Button, Bool, Color, Instance, HasStrictTraits, Str, Enum, on_trait_change
)
from traitsui.api import Item, UItem, VGroup, HGroup, View
from pyface.image_resource import ImageResource

from get_share_path import get_share_path
from utils import get_config

default = {
'resolution': 'fullscreen',
'shadows': 'high',
'antialiasing': 'high',
'difficulty': 'easy',
'ai': 'stockfish',
'user_pieces_color': '255,237,178',
'user_smoke_color': '105,94,59',
'ai_pieces_color': '130,20,20',
'ai_smoke_color': '77,31,102',
'background_color': '255,255,255',
'board_color_1': '179,153,105',
'board_color_2': '255,255,255',
'allowed_move_color': '240,207,87',
}


class ToonChess(HasStrictTraits):
resolution = Enum('fullscreen', '800/600', '1024/000', '1920/1600')
resolution = Enum('fullscreen', '1024/576', '1920/1600')
shadows = Enum('high', 'low', 'very low')
antialiasing = Enum('high', 'low', 'none')

difficulty = Enum('very high', 'high', 'normal', 'easy')
difficulty = Enum('impossible', 'hard', 'normal', 'easy')
ai = Str('stockfish')

user_color = Color()
ai_color = Color()
user_pieces_color = Color()
ai_pieces_color = Color()
user_smoke_color = Color()
ai_smoke_color = Color()
background_color = Color()
board_color_1 = Color()
board_color_2 = Color()
allowed_move_color = Color()

play_button = Button("Play")
reset_button = Button("Reset default")

game_running = Bool(False)
_executor = Instance(futures.ThreadPoolExecutor)

traits_view = View(
VGroup(
HGroup(
@@ -27,26 +59,78 @@ class ToonChess(HasStrictTraits):
label='Graphical settings',
show_border=True
),
'20',
HGroup(
Item('ai', label='AI'),
Item('difficulty'),
label='AI settings',
show_border=True
),
HGroup(
Item('user_color', style='custom'),
Item('ai_color', style='custom'),
'20',
VGroup(
HGroup(
VGroup(
Item('user_pieces_color', label='Pieces'),
Item('user_smoke_color', label='Smoke'),
label='User',
show_border=True
),
VGroup(
Item('ai_pieces_color', label='Pieces'),
Item('ai_smoke_color', label='Smoke'),
label='AI',
show_border=True
),
),
Item('background_color', label='Background'),
Item('board_color_1'),
Item('board_color_2'),
Item('allowed_move_color'),
label='Game colors',
show_border=True
),
HGroup(
UItem('play_button'),
UItem('play_button', enabled_when=("not game_running")),
UItem('reset_button'),
show_border=True
),
show_border=True
),
resizable=True,
resizable=False,
title='ToonChess',
icon=ImageResource(get_share_path() + 'logo.png')
)

def __init__(self, **traits):
config = get_config()

values = {
key: config.get(key, value)
for key, value in default.items()
}
self.set(**values)

super(ToonChess, self).__init__(**traits)

@on_trait_change('play_button')
def _on_play_click(self):
self.game_running = True
future = self._executor.submit(self._play)
future.add_done_callback(self._game_done)

def _play(self):
process = subprocess.Popen("ToonChess")
out, err = process.communicate()

def _game_done(self, future):
self.game_running = False

def __executor_default(self):
return futures.ThreadPoolExecutor(max_workers=1)

@on_trait_change('reset_button')
def _on_reset_click(self):
self.set(**default)

if __name__ == '__main__':
ToonChess().configure_traits()
10 changes: 10 additions & 0 deletions gui/toonchess/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from get_share_path import get_share_path


def get_config():
with open(get_share_path() + 'config.txt', 'r') as fobj:
config = {}
for line in fobj:
key, value = line.split()
config[key] = value
return config
13 changes: 13 additions & 0 deletions share/toonchess/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resolution fullscreen
shadows high
antialiasing high
difficulty easy
ai stockfish
user_pieces_color 255,237,178
user_smoke_color 105,94,59
ai_pieces_color 130,20,20
ai_smoke_color 77,31,102
background_color 255,255,255
board_color_1 179,153,105
board_color_2 255,255,255
allowed_move_color 240,207,87