Skip to content

Commit acd2867

Browse files
authored
Merge pull request #347 from lxgr-linux/feat/342-improve-mousability-for-views
Feat/342 improve mousability for views
2 parents dc05977 + b7cf66a commit acd2867

40 files changed

+489
-262
lines changed

src/pokete/__main__.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
exception_propagating_periodic_event,
2626
)
2727
from pokete.base.input import ACTION_DIRECTIONS, Action, _ev, get_action
28-
from pokete.base.input_loops import ask_bool, ask_text, text_input
28+
from pokete.base.input_loops import (
29+
ask_bool,
30+
ask_text,
31+
)
32+
from pokete.base.input_loops.new_text_input import TextInput
33+
from pokete.base.mouse import mouse_interaction_manager
2934
from pokete.base.periodic_event_manager import PeriodicEventManager
3035
from pokete.base.single_event import single_event_periodic_event
3136
from pokete.base.tss import tss
@@ -363,7 +368,7 @@ def _game(_map: PlayMap, figure: Figure):
363368
notifier.notify("Weather", "Info", _map.weather.info)
364369
while True:
365370
# Directions are not being used yet
366-
action = get_action()
371+
action, _ = get_action()
367372
mouse_choosen = -1
368373
if mvp.movemap.mouse_choosen >= 0:
369374
mouse_choosen = mvp.movemap.mouse_choosen
@@ -384,16 +389,19 @@ def _game(_map: PlayMap, figure: Figure):
384389
_ev.clear()
385390
mvp.movemap.show(init=True)
386391
elif action.triggers(Action.CONSOLE):
387-
inp = text_input(
388-
ctx,
392+
mvp.movemap.code_label.rechar(":")
393+
text_input = TextInput(
389394
mvp.movemap.code_label,
390-
":",
391-
mvp.movemap.width,
395+
mvp.movemap.width - 1,
392396
(mvp.movemap.width - 2) * mvp.movemap.height - 1,
393-
)[1:]
397+
)
398+
mouse_interaction_manager.attach([text_input])
399+
inp = text_input(ctx)
400+
ctx = change_ctx(ctx, mvp.movemap)
394401
mvp.movemap.code_label.outp(figure.map.pretty_name)
395-
codes(inp, figure)
396-
_ev.clear()
402+
if inp is None:
403+
continue
404+
codes(inp[1:], figure)
397405
for statement, x, y in zip(
398406
[
399407
figure.x + 6 > mvp.movemap.x + mvp.movemap.width,

src/pokete/base/color.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Contains the color class"""
2+
23
from os import environ
34

45

56
class Color:
67
"""Color class that provides all needed escape codes"""
8+
79
reset = "\033[0m"
810
thicc = "\033[1m"
911
underlined = "\033[4m"
@@ -30,6 +32,8 @@ class Color:
3032
cavegrey = "\033[38;5;236m"
3133
peach = "\033[38;5;216m"
3234

35+
bg_cyan = "\033[46m"
36+
3337
if environ.get("TERM", "linux") == "linux": # this fixes some colors on TTY
3438
gold = "\033[38;5;9m"
3539
cavegrey = "\033[38;5;7m"

src/pokete/base/input/event.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Contains event var"""
22

3-
from typing import Callable
3+
from typing import Callable, Optional
4+
5+
from pokete.base.input.key import Key
46

57
EmitFn = Callable[[], None]
68

@@ -10,17 +12,17 @@ class Event:
1012
ARGS:
1113
event: Initial char"""
1214

13-
def __init__(self, event=""):
14-
self._ev = event
15+
def __init__(self, event=None):
16+
self._ev: Optional[Key] = event
1517
self.emit_fn: EmitFn
1618

17-
def get(self):
19+
def get(self) -> Optional[Key]:
1820
"""Getter
1921
RETURNS:
2022
Current char"""
2123
return self._ev
2224

23-
def set(self, event):
25+
def set(self, event: Key):
2426
"""Setter
2527
ARGS:
2628
event: New char"""
@@ -29,7 +31,7 @@ def set(self, event):
2931

3032
def clear(self):
3133
"""Clears the event"""
32-
self._ev = ""
34+
self._ev = None
3335

3436
def set_emit_fn(self, emit_fn: EmitFn):
3537
"""Sets the method used to emit events to the timer"""

src/pokete/base/input/hotkeys.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""Contains everything related to input processing"""
22

3+
import logging
34
import sys
45
from collections import defaultdict
56
from enum import Enum, auto
7+
from typing import Optional
68

9+
from pokete.base.input import key
710
from pokete.util import liner
811

912
from .event import _ev
@@ -60,7 +63,7 @@ class Action(Enum):
6063
INTERACT = auto()
6164

6265
@property
63-
def mapping(self):
66+
def mapping(self) -> str:
6467
"""Returns the current mapped char"""
6568
return get_mapping(self, hotkey_mappings)
6669

@@ -102,7 +105,7 @@ def get_x_strength(self) -> int:
102105
ACTION_DIRECTIONS = (Action.LEFT, Action.RIGHT, Action.UP, Action.DOWN)
103106
ACTION_UP_DOWN = (Action.UP, Action.DOWN)
104107

105-
hotkey_mappings = {
108+
hotkey_mappings: dict[str, ActionList] = {
106109
"1": ActionList([Action.ACT_1, Action.DECK, Action.CHOOSE_ATTACK]),
107110
"2": ActionList(
108111
[
@@ -167,12 +170,13 @@ def get_x_strength(self) -> int:
167170
}
168171

169172

170-
def get_mapping(action, keys):
173+
def get_mapping(action: Action, keys: dict[str, ActionList]) -> str:
171174
"""Returns the current mapped char"""
172-
for key, actions_list in keys.items():
175+
for _key, actions_list in keys.items():
173176
if action in actions_list:
174-
return key
175-
return None
177+
return _key
178+
logging.warning("Unset mapping for Action '%s'", action.name)
179+
return ""
176180

177181

178182
def hotkeys_save():
@@ -227,13 +231,16 @@ def hotkeys_from_save(ctx, save, version_change):
227231

228232

229233
# Returns an action, then clears input; all input is valid to read only once
230-
def get_action() -> ActionList:
234+
def get_action() -> tuple[ActionList, Optional[key.Key]]:
231235
"""Returns the current actions list"""
232236
retval = EMPTY_ACTIONLIST
233237
raw_input = _ev.get()
234-
if raw_input == "exit":
238+
if raw_input is None:
239+
return retval, None
240+
if raw_input == key.EXIT:
235241
raise KeyboardInterrupt
236-
if raw_input in hotkey_mappings:
237-
retval = hotkey_mappings[raw_input]
242+
marshall_input = raw_input.marshall()
243+
if marshall_input in hotkey_mappings:
244+
retval = hotkey_mappings[marshall_input]
238245
_ev.clear()
239-
return retval
246+
return retval, raw_input

src/pokete/base/input/key.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Key:
2+
def __init__(self, char="", rep=""):
3+
self.char = char
4+
self.__rep = rep
5+
6+
def marshall(self) -> str:
7+
if self.__rep:
8+
return f"Key.{self.__rep}"
9+
return self.char
10+
11+
def has_char(self) -> bool:
12+
return not not self.char
13+
14+
15+
UP = Key(rep="up")
16+
DOWN = Key(rep="down")
17+
LEFT = Key(rep="left")
18+
RIGHT = Key(rep="right")
19+
ENTER = Key(rep="enter")
20+
BACKSPACE = Key(rep="backspace")
21+
SPACE = Key(" ", rep="space")
22+
ESC = Key(rep="esc")
23+
EXIT = Key()

src/pokete/base/input/recogniser.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,38 @@
66
from pokete.base.exception_propagation.propagating_thread import (
77
PropagatingThread,
88
)
9+
from pokete.base.input import key
910

1011
from .event import _ev
1112
from .mouse import mouse_manager
1213

1314

1415
class Recogniser:
1516
ESCAPES: list[int] = [27, 0]
16-
ESCAPED_KEY_MAPPING: dict[str, str] = {
17-
"[A": "Key.up",
18-
"[B": "Key.down",
19-
"[C": "Key.right",
20-
"[D": "Key.left",
21-
"H": "Key.up",
22-
"P": "Key.down",
23-
"M": "Key.right",
24-
"K": "Key.left",
17+
ESCAPED_KEY_MAPPING: dict[str, key.Key] = {
18+
"[A": key.UP,
19+
"[B": key.DOWN,
20+
"[C": key.RIGHT,
21+
"[D": key.LEFT,
22+
"H": key.UP,
23+
"P": key.DOWN,
24+
"M": key.RIGHT,
25+
"K": key.LEFT,
2526
}
26-
UNIX_KEY_MAPPING: dict[int, str] = {
27-
13: "Key.enter",
28-
127: "Key.backspace",
29-
32: "Key.space",
30-
27: "Key.esc",
31-
3: "exit",
27+
UNIX_KEY_MAPPING: dict[int, key.Key] = {
28+
13: key.ENTER,
29+
127: key.BACKSPACE,
30+
32: key.SPACE,
31+
27: key.ESC,
32+
3: key.EXIT,
3233
}
33-
WINDOWS_KEY_MAPPING: dict[int, str] = {
34-
13: "Key.enter",
35-
127: "Key.backspace",
36-
8: "Key.backspace",
37-
32: "Key.space",
38-
27: "Key.esc",
39-
3: "exit",
34+
WINDOWS_KEY_MAPPING: dict[int, key.Key] = {
35+
13: key.ENTER,
36+
127: key.BACKSPACE,
37+
8: key.BACKSPACE,
38+
32: key.SPACE,
39+
27: key.ESC,
40+
3: key.EXIT,
4041
}
4142

4243
def __init__(self):
@@ -88,12 +89,12 @@ def check_escape(self):
8889
self.__escape_event.clear()
8990
time.sleep(0.01)
9091
if self.__escape_input == "":
91-
_ev.set("Key.esc")
92+
_ev.set(key.ESC)
9293
self.__escape_input = None
9394

94-
def set_event(self, char: str, key_mapping: dict[int, str]):
95+
def set_event(self, char: str, key_mapping: dict[int, key.Key]):
9596
char_ord = ord(char)
96-
key_mapping.setdefault(char_ord, f"{char.rstrip()}")
97+
key_mapping.setdefault(char_ord, key.Key(f"{char.rstrip()}"))
9798
if char_ord in self.ESCAPES:
9899
self.__escape_input = ""
99100
self.__escape_event.set()
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
from .ask import ask_bool, ask_ok, ask_text
2-
from .text_input import text_input

src/pokete/base/input_loops/ask/__init__.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22

33
from threading import Event
44

5+
from scrap_engine.addable.misc.frame import Optional
6+
57
from pokete.base.change import change_ctx
8+
from pokete.base.ui.views.boxes import InfoBoxView, InputBoxView
69

710
from ... import loops
811
from ...context import Context
9-
from ...ui.elements import InfoBox, InputBox
10-
from ..text_input import text_input
1112
from .bool import ask_bool
1213
from .ok import ask_ok
1314

1415

1516
def wait_event(ctx: Context, text: str, event: Event):
1617
"""Shows box until event is set"""
17-
with InfoBox(text, info="", ctx=ctx):
18+
with InfoBoxView(text, info=None, ctx=ctx):
1819
loops.event_wait(ctx, event)
1920

2021

21-
def ask_text(ctx: Context, infotext, introtext, text, name, max_len):
22+
def ask_text(
23+
ctx: Context, infotext, introtext, text, name, max_len
24+
) -> Optional[str]:
2225
"""Asks the player to input a text
2326
ARGS:
2427
ctx: Context
@@ -27,7 +30,11 @@ def ask_text(ctx: Context, infotext, introtext, text, name, max_len):
2730
text: The default text in the text field
2831
name: The boxes displayed name
2932
max_len: Max length of the text"""
30-
with InputBox(infotext, introtext, text, max_len, name, ctx) as inputbox:
33+
34+
"""
35+
with InputBoxView(
36+
infotext, introtext, text, max_len, name, ctx
37+
) as inputbox:
3138
ctx = change_ctx(ctx, inputbox)
3239
ret = text_input(
3340
ctx,
@@ -37,3 +44,5 @@ def ask_text(ctx: Context, infotext, introtext, text, name, max_len):
3744
max_len=max_len,
3845
)
3946
return ret
47+
"""
48+
return InputBoxView(infotext, introtext, text, max_len, name)(ctx)

src/pokete/base/input_loops/ask/bool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __call__(self, ctx: Context) -> bool:
5959
with self.center_add(self.map):
6060
ctx = change_ctx(ctx, self)
6161
while True:
62-
action = get_action()
62+
action, _ = get_action()
6363
if action.triggers(Action.ACCEPT):
6464
ret = True
6565
break

src/pokete/base/input_loops/ask/ok.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __call__(self, ctx: Context):
4949
with self.center_add(self.map):
5050
ctx = change_ctx(ctx, self)
5151
while True:
52-
action = get_action()
52+
action, _ = get_action()
5353
if action.triggers(Action.ACCEPT or action == Action.CANCEL):
5454
break
5555
loops.std(ctx)

0 commit comments

Comments
 (0)