1. Amirouche Boubekki
  2. antiquote

Commits

Amirouche Boubekki  committed cc5f516

added stacked magic mini project

  • Participants
  • Parent commits 865505a
  • Branches default

Comments (0)

Files changed (62)

File stacked-magic/Makefile

View file
+planetcute:
+	echo "fetch planet cute files and unpack!"

File stacked-magic/ipython_log.py

View file
+# IPython log file
+
+

File stacked-magic/main.py

View file
+import random
+
+from kivy.interactive import InteractiveLauncher
+from kivy.app import App
+from kivy.uix.label import Label
+from kivy.uix.relativelayout import RelativeLayout
+from kivy.uix.popup import Popup
+from kivy.uix.layout import Layout
+from kivy.uix.widget import Widget
+from kivy.clock import Clock
+from kivy.graphics import Canvas
+from kivy.logger import Logger
+from kivy.uix.floatlayout import FloatLayout
+
+from kivy.graphics.texture import Texture
+from kivy.graphics import Rectangle
+from kivy.uix.image import Image
+from kivy.base import EventLoop
+
+WOOD_BLOCK = 'static/Wood Block.png'  # 101x171
+
+WOOD_CELL_WIDTH = 101 - 1
+WOOD_CELL_HEIGHT = (171 / 4) * 3 - 10
+
+GEM_BLUE = 'static/Gem Blue.png'
+GEM_GREEN = 'static/Gem Green.png'
+GEM_ORANGE = 'static/Gem Orange.png'
+
+GEMS = (GEM_ORANGE, GEM_GREEN, GEM_BLUE)
+
+GEM_CELL_INITIAL_Y_PADDING = 40
+
+SELECTOR = 'static/Selector.png'
+SELECTOR_INITIAL_Y_PADDING = (171 / 4) - 4
+SELECTOR_Y_MOVE = (171 / 4) * 3 - 10
+
+
+class Board(Widget):
+
+    interesting_keys = {
+#        8: 'backspace',
+#        13: 'enter',
+#        127: 'del',
+#        271: 'enter',
+        273: 'cursor_up',
+        274: 'cursor_down',
+        275: 'cursor_right',
+        276: 'cursor_left',
+#        278: 'cursor_home',
+#        279: 'cursor_end',
+#        280: 'cursor_pgup',
+#        281: 'cursor_pgdown',
+#        303: 'shift_L',
+#        304: 'shift_R',
+    }
+
+
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
+
+        current_x = 0.
+        for x in range(10):
+            current_y = 0  # -(171.1 / 4)
+            for y in range(9):
+                image = Image(
+                    source='static/Wood Block.png', 
+                    pos=(current_x, current_y),
+                    size=(101, 171),
+                    size_hint=(None, None),
+                )
+                self.add_widget(image)
+                current_y += WOOD_CELL_HEIGHT
+            current_x += WOOD_CELL_WIDTH
+        current_x = 1 * WOOD_CELL_WIDTH
+
+        for x in range(8):
+            current_y = WOOD_CELL_HEIGHT * 5 + GEM_CELL_INITIAL_Y_PADDING
+            for y in range(3):
+                gem = random.choice(GEMS)
+                gem = Image(
+                    source=gem, 
+                    pos=(current_x, current_y),
+                    size=(101, 171),
+                    size_hint=(None, None),
+                )
+                self.add_widget(gem)
+                current_y += WOOD_CELL_HEIGHT
+            current_x += WOOD_CELL_WIDTH
+
+        self.cursor = Image(
+            source=SELECTOR, 
+            pos=(WOOD_CELL_WIDTH, SELECTOR_INITIAL_Y_PADDING),
+            size=(101, 171),
+            size_hint=(None, None),
+        )
+
+        self.cursor.column = 1
+        self.cursor.row = 0
+
+        self.add_widget(self.cursor)
+
+        self.focus = True
+        self._win = None
+        self._bind_keyboard()
+
+    def _keyboard_released(self):
+        self.focus = False
+
+    # copy pasted and modified from uix.textinput
+    def _set_window(self, *largs):
+        win = self._win
+        if not win:
+            self._win = win = EventLoop.window
+        if not win:
+            # we got argument, it could be the previous schedule
+            # cancel focus.
+            if len(largs):
+                Logger.warning('Board: '
+                               'Cannot focus the element, unable to get '
+                               'root window')
+                return
+            else:
+                #XXX where do `value` comes from?
+                #XXX wat is value?
+                Clock.schedule_once(partial(self.on_focus, self, value), 0)
+            return
+
+    def on_focus(self, instance, value, *largs):
+        self._set_window(*largs)
+
+        if value:
+            if self.keyboard_mode != 'managed':
+                self._bind_keyboard()
+        else:
+            if self.keyboard_mode != 'managed':
+                self._unbind_keyboard()
+
+    def _unbind_keyboard(self):
+        self._set_window()
+        win = self._win
+        if self._keyboard:
+            keyboard = self._keyboard
+            keyboard.unbind(
+                on_key_down=self._keyboard_on_key_down,
+                on_key_up=self._keyboard_on_key_up
+            )
+            keyboard.release()
+            self._keyboard = None
+
+        self.cancel_selection()
+        Clock.unschedule(self._do_blink_cursor)
+        self._win = None
+
+    def _bind_keyboard(self):
+        self._set_window()
+        win = self._win
+        keyboard = win.request_keyboard(
+            self._keyboard_released, 
+            self, 
+            'text_input'
+        )
+        self._keyboard = keyboard
+        keyboard.bind(
+            on_key_down=self._keyboard_on_key_down,
+            on_key_up=self._keyboard_on_key_up)
+        Clock.schedule_interval(self._do_blink_cursor, 1 / 2.)
+
+    def _do_blink_cursor(self, dt):
+        # # Callback called by the timer to blink the cursor, according to the
+        # # last activity in the widget
+        # b = (Clock.get_time() - self._cursor_blink_time)
+        # self.cursor_blink = int(b * 2) % 2
+        pass
+
+    def _keyboard_on_key_down(self, window, keycode, text, modifiers):
+        # Keycodes on OSX:
+        ctrl, cmd = 64, 1024
+        key, key_str = keycode
+
+        is_interesting_key = key in (list(self.interesting_keys.keys()))
+        if is_interesting_key:
+            k = self.interesting_keys.get(key)
+            if k:
+                key = (None, None, k, 1)
+                self._key_down(key)
+        else:
+            # ignore
+            pass
+
+    def _keyboard_on_key_up(self, window, keycode):
+        key, key_str = keycode
+        k = self.interesting_keys.get(key)
+        if k:
+            key = (None, None, k, 1)
+            self._key_up(key)
+
+    def _key_down(self, key, repeat=False):
+        displayed_str, internal_str, internal_action, scale = key
+        self.do_cursor_movement(internal_action)
+
+    def _key_up(self, key, repeat=False):
+        pass  # nothing to do
+
+    def do_cursor_movement(self, action):
+        '''Move the cursor relative to it's current position.
+        Action can be one of :
+
+            - cursor_left: move the cursor to the left
+            - cursor_right: move the cursor to the right
+            - cursor_down: fetch the all consecutive gems with same color if match
+                           the current color or if there is no current gem.
+        '''
+        if action == 'cursor_right':
+            self.cursor.column += 1
+            self.cursor.x = self.cursor.column * WOOD_CELL_WIDTH
+        elif action == 'cursor_left':
+            self.cursor.column -= 1
+            self.cursor.x = self.cursor.column * WOOD_CELL_WIDTH
+        elif action == 'cursor_down':
+            self.cursor.row -= 1
+            self.cursor.y = SELECTOR_INITIAL_Y_PADDING + self.cursor.row * SELECTOR_Y_MOVE
+        elif action == 'cursor_up':
+            self.cursor.row += 1
+            self.cursor.y = SELECTOR_INITIAL_Y_PADDING + self.cursor.row * SELECTOR_Y_MOVE
+        else:
+            pass  # shouldn't happen
+
+
+from kivy.core.window import Window
+
+from kivy.uix.screenmanager import NoTransition, SlideTransition, SwapTransition
+from kivy.uix.screenmanager import FadeTransition, WipeTransition, FallOutTransition
+from kivy.uix.screenmanager import RiseInTransition
+
+transitions = [NoTransition(), SlideTransition(), SwapTransition(), 
+               FadeTransition(), WipeTransition(), FallOutTransition(),
+               RiseInTransition()]
+
+
+class StackedMagic(App):
+
+    
+    def build(self):
+        from kivy.uix.screenmanager import ScreenManager, Screen
+        from kivy.uix.screenmanager import SwapTransition
+
+        # Create the manager
+        self.sm = sm = ScreenManager(transition=SwapTransition())
+
+        # Add board in a screen
+        screen = Screen(name='Board')
+        board = Board()
+        screen.add_widget(board)
+        sm.add_widget(screen)
+
+        # Add dacode
+        from kivy.uix.textinput import TextInput
+        with open('main.py') as f:
+            dacode = TextInput(text=f.read())
+        screen = Screen(name='dacode')
+        screen.add_widget(dacode)
+        sm.add_widget(screen)
+
+        from kivy.core.window import Window
+        Window.bind(on_touch_down=self.on_touch_down)
+        return sm
+
+    def on_touch_down(self, *args):
+        '''Event called when a touch down event is initiated.
+        '''
+        transition = random.choice(transitions)
+        self.sm.transition = transition
+        print('testing', transition)
+        self.sm.current = next(self.sm)
+
+StackedMagic().run()
+
+# i = InteractiveLauncher(StackedMagic())
+
+# start with ipython -i main.py 
+# then type i.run() in ipython to display the window
+# then start kiving
+
+

File stacked-magic/mockups/ground.png

Added
New image

File stacked-magic/mockups/ground.svg

View file
Added
New image

File stacked-magic/static/Brown Block.png

Added
New image

File stacked-magic/static/Character Boy.png

Added
New image

File stacked-magic/static/Character Cat Girl.png

Added
New image

File stacked-magic/static/Character Horn Girl.png

Added
New image

File stacked-magic/static/Character Pink Girl.png

Added
New image

File stacked-magic/static/Character Princess Girl.png

Added
New image

File stacked-magic/static/Chest Closed.png

Added
New image

File stacked-magic/static/Chest Lid.png

Added
New image

File stacked-magic/static/Chest Open.png

Added
New image

File stacked-magic/static/Dirt Block.png

Added
New image

File stacked-magic/static/Door Tall Closed.png

Added
New image

File stacked-magic/static/Door Tall Open.png

Added
New image

File stacked-magic/static/Enemy Bug.png

Added
New image

File stacked-magic/static/Gem Blue.png

Added
New image

File stacked-magic/static/Gem Green.png

Added
New image

File stacked-magic/static/Gem Orange.png

Added
New image

File stacked-magic/static/Grass Block.png

Added
New image

File stacked-magic/static/Heart.png

Added
New image

File stacked-magic/static/Key.png

Added
New image

File stacked-magic/static/Plain Block.png

Added
New image

File stacked-magic/static/PlanetCuteShadowMockup.jpg

Added
New image

File stacked-magic/static/PlanetCuteShadowTest.png

Added
New image

File stacked-magic/static/Ramp East.png

Added
New image

File stacked-magic/static/Ramp North.png

Added
New image

File stacked-magic/static/Ramp South.png

Added
New image

File stacked-magic/static/Ramp West.png

Added
New image

File stacked-magic/static/Rock.png

Added
New image

File stacked-magic/static/Roof East.png

Added
New image

File stacked-magic/static/Roof North East.png

Added
New image

File stacked-magic/static/Roof North West.png

Added
New image

File stacked-magic/static/Roof North.png

Added
New image

File stacked-magic/static/Roof South East.png

Added
New image

File stacked-magic/static/Roof South West.png

Added
New image

File stacked-magic/static/Roof South.png

Added
New image

File stacked-magic/static/Roof West.png

Added
New image

File stacked-magic/static/Selector.png

Added
New image

File stacked-magic/static/Shadow East.png

Added
New image

File stacked-magic/static/Shadow North East.png

Added
New image

File stacked-magic/static/Shadow North West.png

Added
New image

File stacked-magic/static/Shadow North.png

Added
New image

File stacked-magic/static/Shadow Side West.png

Added
New image

File stacked-magic/static/Shadow South East.png

Added
New image

File stacked-magic/static/Shadow South West.png

Added
New image

File stacked-magic/static/Shadow South.png

Added
New image

File stacked-magic/static/Shadow West.png

Added
New image

File stacked-magic/static/SpeechBubble.png

Added
New image

File stacked-magic/static/Star.png

Added
New image

File stacked-magic/static/Stone Block Tall.png

Added
New image

File stacked-magic/static/Stone Block.png

Added
New image

File stacked-magic/static/Tree Short.png

Added
New image

File stacked-magic/static/Tree Tall.png

Added
New image

File stacked-magic/static/Tree Ugly.png

Added
New image

File stacked-magic/static/Wall Block Tall.png

Added
New image

File stacked-magic/static/Wall Block.png

Added
New image

File stacked-magic/static/Water Block.png

Added
New image

File stacked-magic/static/Window Tall.png

Added
New image

File stacked-magic/static/Wood Block.png

Added
New image