Skip to content

Commit

Permalink
Merge pull request #93 from hanzi/gui-overhaul
Browse files Browse the repository at this point in the history
GUI Overhaul
  • Loading branch information
40Cakes authored Oct 31, 2023
2 parents 4284a0b + d7ccad7 commit 5e8d21b
Show file tree
Hide file tree
Showing 31 changed files with 1,599 additions and 1,357 deletions.
228 changes: 0 additions & 228 deletions import.py

This file was deleted.

37 changes: 0 additions & 37 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,40 +322,3 @@ def load_config_from_directory(path: Path, allow_missing_files=False) -> None:
elif not allow_missing_files:
console.print(f"[bold red]Expected a config file {str(file_path)} could not be found.[/]")
sys.exit(1)


previous_bot_mode: str = ""


def toggle_manual_mode() -> None:
"""
Toggles between manual mode and the bot mode configured in `general.yml`.
If the configured bot mode is already `manual`, this has no effect.
"""
global previous_bot_mode

if config["general"]["bot_mode"] == "manual" and previous_bot_mode != "":
config["general"]["bot_mode"] = previous_bot_mode
previous_bot_mode = ""
else:
previous_bot_mode = config["general"]["bot_mode"]
config["general"]["bot_mode"] = "manual"


def set_bot_mode(new_bot_mode) -> None:
global previous_bot_mode
config["general"]["bot_mode"] = new_bot_mode
previous_bot_mode = ""


def force_manual_mode() -> None:
"""
Ensures that the bot is running in manual mode, to give control back to
the user. If the bot is already in manual mode, this has no effect.
"""
global previous_bot_mode

if config["general"]["bot_mode"] != "manual":
previous_bot_mode = config["general"]["bot_mode"]

config["general"]["bot_mode"] = "manual"
116 changes: 116 additions & 0 deletions modules/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from typing import TYPE_CHECKING, Union

if TYPE_CHECKING:
from modules.gui import PokebotGui
from modules.libmgba import LibmgbaEmulator
from modules.profiles import Profile
from modules.roms import ROM


class BotContext:
def __init__(self, initial_bot_mode: str = 'manual'):
self.emulator: Union['LibmgbaEmulator', None] = None
self.gui: Union['PokebotGui', None] = None
self.profile: Union['Profile', None] = None
self.debug: bool = False

self._current_message: str = ''

self._current_bot_mode: str = initial_bot_mode
self._previous_bot_mode: str = 'manual'

@property
def message(self) -> str:
return self._current_message

@message.setter
def message(self, new_message: str) -> None:
self._current_message = new_message
self._update_gui()

@property
def emulation_speed(self) -> float:
if not self.emulator.get_throttle():
return 0
else:
return self.emulator.get_speed_factor()

@emulation_speed.setter
def emulation_speed(self, new_speed: float) -> None:
if self.emulator:
if new_speed == 0:
self.emulator.set_throttle(False)
else:
self.emulator.set_throttle(True)
self.emulator.set_speed_factor(new_speed)
self._update_gui()

@property
def bot_mode(self) -> str:
return self._current_bot_mode

@bot_mode.setter
def bot_mode(self, new_bot_mode: str) -> None:
if self._current_bot_mode != new_bot_mode:
self._previous_bot_mode = self._current_bot_mode
self._current_bot_mode = new_bot_mode
self._update_gui()

def toggle_manual_mode(self) -> None:
if self._current_bot_mode == "manual":
self._current_bot_mode = self._previous_bot_mode
self._previous_bot_mode = "manual"
else:
self._previous_bot_mode = self._current_bot_mode
self._current_bot_mode = "manual"
self._update_gui()

@property
def audio(self) -> bool:
if self.emulator:
return self.emulator.get_audio_enabled()
else:
return False

@audio.setter
def audio(self, audio_on: bool) -> None:
if self.emulator:
self.emulator.set_audio_enabled(audio_on)
self._update_gui()

def toggle_audio(self) -> None:
if self.emulator:
self.emulator.set_audio_enabled(not self.emulator.get_audio_enabled())
self._update_gui()

@property
def video(self) -> bool:
if self.emulator:
return self.emulator.get_video_enabled()
else:
return False

@video.setter
def video(self, video_on: bool) -> None:
if self.emulator:
self.emulator.set_video_enabled(video_on)
self._update_gui()

def toggle_video(self):
if self.emulator:
self.emulator.set_video_enabled(not self.emulator.get_video_enabled())
self._update_gui()

@property
def rom(self) -> Union['ROM', None]:
if self.profile:
return self.profile.rom
else:
return None

def _update_gui(self) -> None:
if self.gui:
self.gui.on_settings_updated()


context = BotContext()
Loading

0 comments on commit 5e8d21b

Please sign in to comment.