/kaleidoscope/scenarios/choose/client.py

https://github.com/yamartin/kaleidoscope · Python · 147 lines · 117 code · 20 blank · 10 comment · 3 complexity · 5799ecaa0208738fd7fe9f4869bba411 MD5 · raw file

  1. from os.path import dirname
  2. from kaleidoscope.scenario import KalScenarioClient
  3. from kivy.properties import NumericProperty, BooleanProperty
  4. from kivy.uix.button import Button
  5. from kivy.uix.label import Label
  6. from kivy.core.window import Window
  7. from kivy.lang import Builder
  8. from kivy.resources import resource_add_path
  9. from functools import partial
  10. #background = Image(join(dirname(__file__), 'background.png'))
  11. resource_add_path(dirname(__file__))
  12. Builder.load_string('''
  13. <PlaceButton>:
  14. size_hint: None, None
  15. font_size: 24
  16. halign: 'center'
  17. canvas.before:
  18. Color:
  19. rgba: (.5, .5, .5, .5) if not self.valid else [(.5, .5, .5, .5), (.5882, .7450, .1450, 1), (.9019, .2745, .121, 1), (.5058, .7921, .7843, 1), (.4980, .2235, .5450, 1)][self.idx]
  20. BorderImage:
  21. source: 'buttonbackground.png'
  22. pos: self.pos
  23. size: self.size
  24. canvas:
  25. Clear
  26. Color:
  27. rgba: self.color
  28. Rectangle:
  29. texture: self.texture
  30. size: self.texture_size
  31. pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1] / 2.)
  32. <ChooseLabel>:
  33. font_size: 24
  34. anchor_x: 'center'
  35. anchor_y: 'middle'
  36. size_hint: None, None
  37. font_name: 'fonts/myriad.ttf'
  38. ''')
  39. class PlaceButton(Button):
  40. valid = BooleanProperty(True)
  41. idx = NumericProperty(0)
  42. class ChooseLabel(Label):
  43. pass
  44. class ChooseClient(KalScenarioClient):
  45. def handle_place(self, args):
  46. '''Select a placement on the table
  47. '''
  48. def place_press(idx, *largs):
  49. self.send('PLACE %d' % idx)
  50. available = map(int, args.split())
  51. cx, cy = Window.center
  52. s = 200
  53. m = 10
  54. self.container.clear_widgets()
  55. self.container.add_widget(
  56. ChooseLabel(text='Choisis une couleur',
  57. pos=(0, cy + 200),
  58. size=(Window.width, 100)
  59. ))
  60. for idx, px, py in ((1, cx-s-m, cy-s-m), (2, cx+m, cy-s-m),
  61. (3, cx-s-m, cy+m), (4, cx+m, cy+m)):
  62. valid = idx in available
  63. button = PlaceButton(text='', size=(200, 200),
  64. pos=(px, py), idx=idx, valid=valid)
  65. self.container.add_widget(button)
  66. if not valid:
  67. continue
  68. button.bind(on_release=partial(place_press, idx))
  69. def handle_scenario(self, args):
  70. '''Select the scenario
  71. '''
  72. def scenario_press(scenario, *largs):
  73. self.send('SCENARIO %s' % scenario)
  74. cx, cy = Window.center
  75. s = 200
  76. m = 10
  77. self.container.clear_widgets()
  78. self.container.add_widget(
  79. ChooseLabel(text='Choisis un scenario',
  80. pos=(0, cy + 200),
  81. size=(Window.width, 100)
  82. ))
  83. py = cy
  84. for scenario, name in (
  85. ('pentaminos', 'Pentaminos'),
  86. ('revolution', 'Revolution'),
  87. #('anglais', 'Anglais')
  88. ):
  89. button = PlaceButton(text=name, size=(350, 100),
  90. pos=(cx - 350 / 2., py - 100))
  91. self.container.add_widget(button)
  92. py += 100 + m * 2
  93. button.bind(on_release=partial(scenario_press, scenario))
  94. def handle_beready(self, args):
  95. '''Be ready !
  96. '''
  97. def beready_press(*largs):
  98. self.send('READY')
  99. cx, cy = Window.center
  100. self.container.clear_widgets()
  101. self.container.add_widget(
  102. ChooseLabel(text=u'Clique lorsque tout le monde a une place',
  103. pos=(0, cy + 200),
  104. size=(Window.width, 100)
  105. ))
  106. button = PlaceButton(text=u'Je suis pr\xeat',
  107. size=(350, 100),
  108. pos=(cx - 350 / 2., cy - 100))
  109. button.bind(on_release=beready_press)
  110. self.container.add_widget(button)
  111. def handle_wait(self, args):
  112. '''Wait someone
  113. '''
  114. cx, cy = Window.center
  115. self.container.clear_widgets()
  116. self.container.add_widget(
  117. ChooseLabel(text='Attends les autres joueurs',
  118. pos=(0, cy + 200),
  119. size=(Window.width, 100)
  120. ))
  121. self.container.add_widget(
  122. ChooseLabel(text=args,
  123. pos=(0, cy - 50),
  124. size=(Window.width, 100)
  125. ))
  126. scenario_class = ChooseClient