Skip to content

Commit

Permalink
wrote FixedLayout kivy class
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnAD committed Feb 11, 2017
1 parent c62d5c6 commit 60d559d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
59 changes: 59 additions & 0 deletions fixedlayout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from kivy.uix.scatterlayout import ScatterLayout
from kivy.properties import ObjectProperty
from kivy.core.window import Window

class FixedLayout(ScatterLayout):

screensize = ObjectProperty((1920, 1080))

def __init__(self, **kw):
kw['size'] = (Window.width, Window.height)
kw['size_hint'] = (None, None)
kw['do_scale'] = False
kw['do_translation'] = False
kw['do_rotation'] = False
super(FixedLayout, self).__init__(**kw)
self.init_diff = [
self.screensize[0]-Window.width,
self.screensize[1]-Window.height
]
self.__fit_to_window()
Window.bind(system_size=self.__fit_to_window)

def __fit_to_window(self, *args):
window_ratio = Window.width/float(Window.height)
if window_ratio>1:
self.__fit_to_landscape_window()
else:
self.__fit_to_portrait_window()


def __fit_to_landscape_window(self, *args):
self.rotation = 0
screen_ratio = self.screensize[0]/float(self.screensize[1])
window_ratio = Window.width/float(Window.height)
if screen_ratio > window_ratio:
self.scale = Window.width/float(self.screensize[0])
else:
self.scale = Window.height/float(self.screensize[1])
frame_left = (self.init_diff[0]/2)*self.scale
frame_bottom = (self.init_diff[1]/2)*self.scale
self.center = (
Window.width/2-frame_left,
Window.height/2-frame_bottom
)

def __fit_to_portrait_window(self, *args):
self.rotation = -90
screen_ratio = self.screensize[1]/float(self.screensize[0])
window_ratio = Window.width/float(Window.height)
if screen_ratio > window_ratio:
self.scale = Window.width/float(self.screensize[1])
else:
self.scale = Window.height/float(self.screensize[0])
frame_left = (self.init_diff[1]/2)*self.scale
frame_bottom = (self.init_diff[0]/2)*self.scale
self.center = (
Window.width/2-frame_left,
Window.height/2+frame_bottom
)
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget

from fixedlayout import FixedLayout

__version__ = '0.0.1'

class GameScreen(Screen):
Expand Down
8 changes: 4 additions & 4 deletions mancala.kv
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ MancalaScreenManagement:

<GameScreen>
name: "game_screen"
RelativeLayout:
FixedLayout:
canvas:
Rectangle:
source: 'assets/img/birch-game-board.png'
pos: self.pos
size: self.size
pos: (0, 0)
size: (1920, 1080)

<SettingsScreen>
name: "settings_screen"
RelativeLayout:
FixedLayout:
canvas:
Rectangle:
source: 'assets/img/settings.png'
Expand Down

0 comments on commit 60d559d

Please sign in to comment.