From 355a9de14bc34ed6bac7511aca26efa6fc42985d Mon Sep 17 00:00:00 2001 From: Daniel afx Date: Tue, 29 Sep 2020 05:10:13 +0300 Subject: [PATCH] split project to modules and experiment with mapview flower --- .gitignore | 32 +---------- README.md | 1 + forestmapview.kv | 18 ++++++ forestmapview.py | 4 ++ game.py | 70 ++++++++++++++++++++++++ gpsblinker.kv | 1 + gpsblinker.py | 7 +++ gpshelper.py | 12 ++++ main.kv | 140 ++--------------------------------------------- main.py | 130 ++++++------------------------------------- settings.kv | 127 ++++++++++++++++++++++++++++++++++++++++++ settings.py | 31 +++++++++++ 12 files changed, 295 insertions(+), 278 deletions(-) create mode 100644 forestmapview.kv create mode 100644 forestmapview.py create mode 100644 game.py create mode 100644 gpsblinker.kv create mode 100644 gpsblinker.py create mode 100644 gpshelper.py create mode 100644 settings.kv create mode 100644 settings.py diff --git a/.gitignore b/.gitignore index 64b00d3..37799c5 100644 --- a/.gitignore +++ b/.gitignore @@ -57,19 +57,6 @@ coverage.xml *.mo *.pot -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - # Sphinx documentation docs/_build/ @@ -112,20 +99,5 @@ ENV/ env.bak/ venv.bak/ -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ +# MapView Cache +cache/ diff --git a/README.md b/README.md index 6fd9d56..692118a 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ A map-based social gardening - `./setup_venv.sh pre` - same as above, for `python 3.8` in `ubuntu 20.04` where we cannot use the stable `kivy 1.11.1` yet - `source venv/bin/activate` - enter the virtual environment +- `garden install mapview` - `./build.sh` - run buildozer inside the virtual env to build and deploy - `./cleanup.sh` - removes the work dirs from the local repo diff --git a/forestmapview.kv b/forestmapview.kv new file mode 100644 index 0000000..43a3332 --- /dev/null +++ b/forestmapview.kv @@ -0,0 +1,18 @@ +#:import MapView kivy.garden.mapview.MapView +#:import GpsBlinker gpsblinker.GpsBlinker +#:include gpsblinker.kv + +: + lat: 42.131331 + lon: 24.747571 + zoom: 18 + on_zoom: + self.zoom = 15 if self.zoom < 15 else self.zoom + on_lat: + pass + on_lon: + pass + GpsBlinker: + lat: root.lat + lon: root.lon + id: blinker \ No newline at end of file diff --git a/forestmapview.py b/forestmapview.py new file mode 100644 index 0000000..df2326a --- /dev/null +++ b/forestmapview.py @@ -0,0 +1,4 @@ +from kivy.garden.mapview import MapView + +class ForestMapView(MapView): + pass \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..0952ffe --- /dev/null +++ b/game.py @@ -0,0 +1,70 @@ +from kivy.uix.widget import Widget + +def collides(rect1, rect2): + r1x = rect1[0][0] + r1y = rect1[0][1] + r2x = rect2[0][0] + r2y = rect2[0][1] + r1w = rect1[1][0] + r1h = rect1[1][1] + r2w = rect2[1][0] + r2h = rect2[1][1] + + if (r1x < r2x + r2w and r1x + r1w > r2x and r1y < r2y + r2h and r1y + r1h > r2y): + return True + else: + return False + +class GameWidget(Widget): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self._keyboard = Window.request_keyboard(self._on_keyboard_closed, self) + self._keyboard.bind(on_key_down=self._on_key_down) + self._keyboard.bind(on_key_up=self._on_key_up) + + self.nearbyobj = [] + + #for obj in range(10): + + with self.canvas: + self.player = Rectangle(source="assets/images/player.png", pos=(0,0), size=(100,100)) + self.enemy = Rectangle(pos=(300,300), size=(80,80)) + + self.keysPressed = set() + + Clock.schedule_interval(self.move_step,0) + + def _on_keyboard_closed(self): + self._keyboard.unbind(on_key_down=self._on_key_down) + self._keyboard.unbind(on_key_up=self._on_key_up) + self._keyboard = None + + def _on_key_down(self, keyboard, keycode, text, modifiers): + self.keysPressed.add(text) + + def _on_key_up(self,keyboard,keycode): + text = keycode[1] + if text in self.keysPressed: + self.keysPressed.remove(text) + + def move_step(self,dt): + currentx = self.player.pos[0] + currenty = self.player.pos[1] + + step_size = 200 * dt + + if "w" in self.keysPressed: + currenty += step_size + if "s" in self.keysPressed: + currenty -= step_size + if "a" in self.keysPressed: + currentx -= step_size + if "d" in self.keysPressed: + currentx += step_size + + self.player.pos = (currentx, currenty) + + if collides((self.player.pos,self.player.size),(self.enemy.pos,self.size)): + print("X") + else: + print('.') \ No newline at end of file diff --git a/gpsblinker.kv b/gpsblinker.kv new file mode 100644 index 0000000..518466e --- /dev/null +++ b/gpsblinker.kv @@ -0,0 +1 @@ +: \ No newline at end of file diff --git a/gpsblinker.py b/gpsblinker.py new file mode 100644 index 0000000..1ad1c99 --- /dev/null +++ b/gpsblinker.py @@ -0,0 +1,7 @@ +from kivy.garden.mapview import MapMarker + +class GpsBlinker(MapMarker): + def blink(self): + # Animantion that changes the blink size and opacity + # When the animation completes, reset the animation, then repeat + pass \ No newline at end of file diff --git a/gpshelper.py b/gpshelper.py new file mode 100644 index 0000000..aed1dcc --- /dev/null +++ b/gpshelper.py @@ -0,0 +1,12 @@ +from kivy.app import App + +class GpsHelper(): + def run(self): + # Start blinking the GpsBlinker + + # Get a reference to GpsBlinker, then call blink() + #gps_blinker = App.get_running_app().root.ids.mapview.ids.blinker + #gps_blinker.blink() + + # Request permission on Android + pass \ No newline at end of file diff --git a/main.kv b/main.kv index b2c2022..5fdd8db 100644 --- a/main.kv +++ b/main.kv @@ -1,137 +1,5 @@ -#: import sm kivy.uix.screenmanager -#: import CheckBox kivy.uix.checkbox +#:include forestmapview.kv - - color: 1, 1, 1, 1 - -: - size_hint: .5, .5 - auto_dismiss: False - title: "The Popup" - Button: - text: "Close" - on_press: root.dismiss() - -SampleBoxLayout: - -: - orientation: "vertical" - padding: 10 - spacing: 10 - - BoxLayout: - orientation: "horizontal" - size_hint_x: .22 - CustLabel: - text: "Are you over 18" - size_hint_x: .80 - CheckBox: - on_active: root.checkbox_18_clicked(self, self.active) - size_hint_x: .55 - - BoxLayout: - orientation: "horizontal" - size_hint_x: .55 - CustLabel: - text: "Favorite Color" - size_hint_x: .265 - CheckBox: - group: "fav_color" - value: root.blue - size_hint_x: .05 - CustLabel: - text: "Blue" - size_hint_x: .15 - CheckBox: - group: "fav_color" - value: root.red - size_hint_x: .05 - CustLabel: - text: "Red" - size_hint_x: .15 - CheckBox: - group: "fav_color" - value: root.green - size_hint_x: .05 - CustLabel: - text: "Green" - size_hint_x: .15 - - BoxLayout: - orientation: "horizontal" - height: 30 - - BoxLayout: - orientation: "horizontal" - size_hint_x: .25 - CustLabel: - text: str(slider_id.value) - Slider: - id: slider_id - min: -100 - max: 100 - value: 0 - step: 1 - - BoxLayout: - orientation: "horizontal" - size_hint_x: .25 - CustLabel: - text: "On / Off" - Switch: - id: switch_id - on_active: root.switch_on(self, self.active) - - BoxLayout: - orientation: "horizontal" - height: 30 - - BoxLayout: - orientation: "horizontal" - size_hint_x: .25 - Button: - text: "Open Popup" - on_press: root.open_popup() - - BoxLayout: - orientation: "horizontal" - size_hint_x: .25 - Spinner: - text: "First" - values: ["First", "Second", "Third"] - id: spinner_id - on_text: root.spinner_clicked(spinner_id.text) - - BoxLayout: - orientation: "horizontal" - size_hint_x: 30 - - BoxLayout: - orientation: "horizontal" - size_hint_x: .25 - TabbedPanel: - do_default_tab: False - TabbedPanelItem: - text: "1st Tab" - Label: - text: "Content of First Panel" - color: 1, 1, 1, 1 - TabbedPanelItem: - text: "2nd Tab" - Label: - text: "Content of Second Panel" - color: 1, 1, 1, 1 - TabbedPanelItem: - text: "3rd Tab" - Label: - text: "Content of Third Panel" - color: 1, 1, 1, 1 - -: - transition: sm.FadeTransition() - MainScreen: - AboutScreen: - -: - -: \ No newline at end of file +ForestMapView: + orientation: 'vertical' + id: mapview \ No newline at end of file diff --git a/main.py b/main.py index 661f262..19dfe7e 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,8 @@ +# Import system modules from random import randrange +# Import Kivy modules +#from kivy.app import App from kivy.app import App from kivy.clock import Clock from kivy.lang import Builder @@ -7,14 +10,17 @@ from kivy.lang import Builder from kivy.uix.screenmanager import Screen, ScreenManager from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label -from kivy.uix.popup import Popup -from kivy.uix.widget import Widget -from kivy.properties import ObjectProperty from kivy.core.window import Window from kivy.graphics import Rectangle +# Import local modules +from forestmapview import ForestMapView +from gpshelper import GpsHelper +from settings import SampleBoxLayout +from game import GameWidget + Builder.load_string(""" : BoxLayout: @@ -41,125 +47,25 @@ class ScreenOne(Screen): class ScreenTwo(Screen): pass -screen_manager = ScreenManager() -screen_manager.add_widget(ScreenOne(name="screen_one")) -screen_manager.add_widget(ScreenTwo(name="screen_two")) - -def collides(rect1, rect2): - r1x = rect1[0][0] - r1y = rect1[0][1] - r2x = rect2[0][0] - r2y = rect2[0][1] - r1w = rect1[1][0] - r1h = rect1[1][1] - r2w = rect2[1][0] - r2h = rect2[1][1] - - if (r1x < r2x + r2w and r1x + r1w > r2x and r1y < r2y + r2h and r1y + r1h > r2y): - return True - else: - return False - -class GameWidget(Widget): - def __init__(self, **kwargs): - super().__init__(**kwargs) - self._keyboard = Window.request_keyboard(self._on_keyboard_closed, self) - self._keyboard.bind(on_key_down=self._on_key_down) - self._keyboard.bind(on_key_up=self._on_key_up) - - self.nearbyobj = [] - - #for obj in range(10): - - with self.canvas: - self.player = Rectangle(source="assets/images/player.png", pos=(0,0), size=(100,100)) - self.enemy = Rectangle(pos=(300,300), size=(80,80)) - - self.keysPressed = set() - - Clock.schedule_interval(self.move_step,0) - - def _on_keyboard_closed(self): - self._keyboard.unbind(on_key_down=self._on_key_down) - self._keyboard.unbind(on_key_up=self._on_key_up) - self._keyboard = None - - def _on_key_down(self, keyboard, keycode, text, modifiers): - self.keysPressed.add(text) - - def _on_key_up(self,keyboard,keycode): - text = keycode[1] - if text in self.keysPressed: - self.keysPressed.remove(text) - - def move_step(self,dt): - currentx = self.player.pos[0] - currenty = self.player.pos[1] - - step_size = 200 * dt - - if "w" in self.keysPressed: - currenty += step_size - if "s" in self.keysPressed: - currenty -= step_size - if "a" in self.keysPressed: - currentx -= step_size - if "d" in self.keysPressed: - currentx += step_size - - self.player.pos = (currentx, currenty) - - if collides((self.player.pos,self.player.size),(self.enemy.pos,self.size)): - print("X") - else: - print('.') - class GlobalForest(App): def build(self): #return Label(text='Hello world') return GameWidget() -class CustomPopup(Popup): - pass - -class SampleBoxLayout(BoxLayout): - checkbox_is_active = ObjectProperty(False) - def checkbox_18_clicked(self, instance, value): - if value is True: - print("Checkbox Checked") - else: - print("Checkbox is Unchecked") - - blue = ObjectProperty(True) - red = ObjectProperty(False) - green = ObjectProperty(False) - - def switch_on(self, instance, value): - if value is True: - print("Switch On") - else: - print("Switch Off") - - def open_popup(self): - the_popup = CustomPopup() - the_popup.open() - - def spinner_clicked(self, value): - print("Spinner Value " + value) - class MainApp(App): def build(self): - #Window.clearcolor = (1, 1, 1, 1) - return screen_manager + pass + #Window.clearcolor = (0, 0, 0, 0) + #return SampleBoxLayout() - global app, screens - app = self - screens = {'main_screen': MainScreen(name='main_screen'), - 'about_screen': AboutScreen(name='about_screen')} - self.screen_manager = ScreenManager - self.main() + def on_start(self): + #Initialize GPS + GpsHelper().run() if __name__ == '__main__': + #screen_manager = ScreenManager() + #screen_manager.add_widget(ScreenOne(name="screen_one")) + #screen_manager.add_widget(ScreenTwo(name="screen_two")) #app = GlobalForest() app = MainApp() app.run() diff --git a/settings.kv b/settings.kv new file mode 100644 index 0000000..640a464 --- /dev/null +++ b/settings.kv @@ -0,0 +1,127 @@ +#: import CheckBox kivy.uix.checkbox + + + color: 1, 1, 1, 1 + +: + size_hint: .5, .5 + auto_dismiss: False + title: "The Popup" + Button: + text: "Close" + on_press: root.dismiss() + +SampleBoxLayout: + +: + orientation: "vertical" + padding: 10 + spacing: 10 + + BoxLayout: + orientation: "horizontal" + size_hint_x: .22 + CustLabel: + text: "Are you over 18" + size_hint_x: .80 + CheckBox: + on_active: root.checkbox_18_clicked(self, self.active) + size_hint_x: .55 + + BoxLayout: + orientation: "horizontal" + size_hint_x: .55 + CustLabel: + text: "Favorite Color" + size_hint_x: .265 + CheckBox: + group: "fav_color" + value: root.blue + size_hint_x: .05 + CustLabel: + text: "Blue" + size_hint_x: .15 + CheckBox: + group: "fav_color" + value: root.red + size_hint_x: .05 + CustLabel: + text: "Red" + size_hint_x: .15 + CheckBox: + group: "fav_color" + value: root.green + size_hint_x: .05 + CustLabel: + text: "Green" + size_hint_x: .15 + + BoxLayout: + orientation: "horizontal" + height: 30 + + BoxLayout: + orientation: "horizontal" + size_hint_x: .25 + CustLabel: + text: str(slider_id.value) + Slider: + id: slider_id + min: -100 + max: 100 + value: 0 + step: 1 + + BoxLayout: + orientation: "horizontal" + size_hint_x: .25 + CustLabel: + text: "On / Off" + Switch: + id: switch_id + on_active: root.switch_on(self, self.active) + + BoxLayout: + orientation: "horizontal" + height: 30 + + BoxLayout: + orientation: "horizontal" + size_hint_x: .25 + Button: + text: "Open Popup" + on_press: root.open_popup() + + BoxLayout: + orientation: "horizontal" + size_hint_x: .25 + Spinner: + text: "First" + values: ["First", "Second", "Third"] + id: spinner_id + on_text: root.spinner_clicked(spinner_id.text) + + BoxLayout: + orientation: "horizontal" + size_hint_x: 30 + + BoxLayout: + orientation: "horizontal" + size_hint_x: .25 + TabbedPanel: + do_default_tab: False + TabbedPanelItem: + text: "1st Tab" + Label: + text: "Content of First Panel" + color: 1, 1, 1, 1 + TabbedPanelItem: + text: "2nd Tab" + Label: + text: "Content of Second Panel" + color: 1, 1, 1, 1 + TabbedPanelItem: + text: "3rd Tab" + Label: + text: "Content of Third Panel" + color: 1, 1, 1, 1 diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..0e3d6d7 --- /dev/null +++ b/settings.py @@ -0,0 +1,31 @@ +from kivy.uix.boxlayout import BoxLayout +from kivy.uix.popup import Popup +from kivy.properties import ObjectProperty + +class CustomPopup(Popup): + pass + +class SampleBoxLayout(BoxLayout): + checkbox_is_active = ObjectProperty(False) + def checkbox_18_clicked(self, instance, value): + if value is True: + print("Checkbox Checked") + else: + print("Checkbox is Unchecked") + + blue = ObjectProperty(True) + red = ObjectProperty(False) + green = ObjectProperty(False) + + def switch_on(self, instance, value): + if value is True: + print("Switch On") + else: + print("Switch Off") + + def open_popup(self): + the_popup = CustomPopup() + the_popup.open() + + def spinner_clicked(self, value): + print("Spinner Value " + value) \ No newline at end of file