add inputs and tests WIP

This commit is contained in:
cecilkorik 2024-02-04 17:42:18 -05:00
parent 1e814ba553
commit 3a643d9864
9 changed files with 86 additions and 12 deletions

15
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: pyglet_demo",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/pyglet_demo.py",
"console": "integratedTerminal"
}
]
}

View file

@ -1,3 +1,6 @@
from . import data
from . import config
from . import screen
from . import screen
from . import control
from . import input
from . import binding

0
engine/binding.py Normal file
View file

View file

@ -5,9 +5,10 @@ import sys
from . import data
class SettingsLoader(UserDict):
def __init__(self: object, file: str) -> None:
def __init__(self: object, files: list) -> None:
self.data = {}
self.load_settings_file(file)
self.files = files
self.reload()
def load_settings_file(self: object, file: str) -> None:
ext = os.path.splitext(file)[1]
@ -16,6 +17,11 @@ class SettingsLoader(UserDict):
self.data.update(json.load(fd))
else:
raise ValueError("Cannot open this type of settings file")
def reload(self: object) -> None:
self.data = {}
for f in self.files:
self.load_settings_file(f)
class SettingsReadOnly(SettingsLoader):
pass
@ -26,4 +32,4 @@ class SettingsMutable(SettingsLoader):
class SettingsWritable(SettingsMutable):
pass
init = SettingsWritable('init.json')
init = SettingsWritable(['init.json'])

21
engine/control.py Normal file
View file

@ -0,0 +1,21 @@
# an input is something the player does
# it can be instantaneous (keydown, keyup)
# it can be have duration (keypressed)
# it can be analog (joystick)
# it can be positional (mouse, touch)
# a control is a function the game provides
# it allows external control over the game
# it
# a binding is the configuration for which inputs map to which controls
# it can be 1-to-1
# it can be left unmapped
# it may not apply in all contexts
# a context is a state in which a particular set of bindings are active
# it is a tree
# multiple contexts can be active, but only one will match and order matters
# it can add bindings, remove bindings, or redefine bindings from the context above it
# the root of the tree represents bindings that are universally active (unless removed)
# different branches of the tree apply to different sections of the game
# for example, an inventory screen may have its own context with additional or redefined bindings
# several context branches may inherit bindings from a common parent

0
engine/input.py Normal file
View file

View file

@ -1,16 +1,25 @@
import pyglet
from . import config
def auto_res(res) -> list:
return res
def auto_res(res: list) -> list:
return res
def get_config() -> dict:
return config.init
def init_window() -> None:
res = [config.init.get('width'), config.init.get('height')]
fs = config.init.get('fullscreen')
borderless = config.init.get('borderless')
scaling = config.init.get('scaling')
global window
if fs and not borderless:
cfg = get_config()
res = [cfg.get('width'), cfg.get('height')]
fs = cfg.get('fullscreen')
borderless_fullscreen = cfg.get('borderless_fullscreen')
borderless_window = cfg.get('borderless_window')
scaling = cfg.get('scaling')
vsync = cfg.get('vsync')
if fs and not borderless_fullscreen:
# real fullscreen mode is requested
# that means we need to carefully select
# an available resolution
@ -18,4 +27,4 @@ def init_window() -> None:
pyglet.window.Window()
window = pyglet.window.Window()

View file

@ -1,7 +1,20 @@
import pyglet
import engine
import unittest
unittest.main()
engine.screen.init_window()
label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
@window.event
def on_draw():
engine.screen.window.clear()
label.draw()
pyglet.app.run()

7
tests/test_settings.py Normal file
View file

@ -0,0 +1,7 @@
import unittest
import engine
class TestSettings(unittest.TestCase):
def test_empty_settings(self: object) -> None:
engine.screen.auto_res([None, None])