Browse Source

Improved text field in python interface, added images

master
Lev 3 years ago
parent
commit
3cef4b50d2
  1. 18
      Cargo.lock
  2. 19
      Cargo.toml
  3. BIN
      degeon_py/degeon_core.so
  4. 12
      degeon_py/input_field.py
  5. 50
      degeon_py/utils.py
  6. 3
      images/dependency_graph.svg
  7. 34
      images/explanation.svg

18
Cargo.lock generated

@ -1565,6 +1565,24 @@ dependencies = [
[[package]]
name = "ironforce_degeon"
version = "0.1.0"
dependencies = [
"base64",
"chrono",
"core-error",
"futures",
"iced",
"iced_native",
"include_optional",
"rand",
"rand_os",
"rayon",
"rsa",
"serde",
"serde_cbor",
"serde_json",
"sha2",
"spin 0.9.2",
]
[[package]]
name = "itoa"

19
Cargo.toml

@ -12,3 +12,22 @@ members = ["degeon", "degeon_core", "ironforce"]
name = "ironforce"
path = "ironforce/src/lib.rs"
[dependencies]
rand_os = "*"
# x25519-dalek = "0.6.0"
# ed25519-dalek = { version = "1.0", features = ["serde"] }
sha2 = "0.8.1"
rand = "*"
rsa = { version = "0.5", features = ["serde"] }
serde = { version = "1.0", features = ["derive", "alloc"], default-features = false }
rayon = { version = "1.5.1", optional = true }
core-error = "0.0.1-rc4"
serde_cbor = "0.11.2"
serde_json = "1.0.72"
spin = "0.9.2"
base64 = "0.13.0"
include_optional = "1.0.1"
iced = { version = "0.3.0", features = ["image"] }
futures = "0.3.18"
iced_native = "0.4.0"
chrono = "0.4.19"

BIN
degeon_py/degeon_core.so

Binary file not shown.

12
degeon_py/input_field.py

@ -1,8 +1,10 @@
import time
import typing
import pygame
from config import font, MESSAGE_HEIGHT, WIDTH, CHAT_SELECTOR_WIDTH, HEIGHT, WHITE, DARKER_BLUE, GREY
from utils import render_text
from dataclasses import dataclass
@ -39,14 +41,7 @@ class TextField:
)
)
if self.value:
message_text: pygame.Surface = font.render(self.value, True, WHITE)
surface.blit(
message_text,
(
40, # or, if we want to center, `(self.width - message_text.get_width()) // 2,`
(self.height - message_text.get_height()) // 2
)
)
render_text(surface, (10, 10), font, self.value, WHITE, self.cursor_position if time.time() % 1 < 0.5 else None)
return surface
def process_event(self, event: pygame.event.Event):
@ -69,6 +64,7 @@ class TextField:
elif event.unicode:
self.value = self.value[:self.cursor_position] + event.unicode + self.value[self.cursor_position:]
self.cursor_position += 1
self.cursor_position = max(0, min(self.cursor_position, len(self.value)))
def collect(self) -> str:
"""

50
degeon_py/utils.py

@ -0,0 +1,50 @@
from typing import Optional
import typing
import pygame
def draw_cursor(surface, cur_position, high, c, size=4):
pygame.draw.line(surface, c,
cur_position,
(cur_position[0], cur_position[1] + high), size)
def get_word_length(ft, word: str):
word_surface = ft.render(word, 0, pygame.Color('black'))
return word_surface.get_size()
def render_text(surface, position: typing.Tuple[int, int], font: pygame.font, text: str, c=pygame.Color('black'),
cursor_position: Optional[int] = None):
"""Render text with hyphenation"""
if len(text) == 0:
return None
need_cursor = cursor_position is not None
lines = [word.split() for word in text.splitlines()]
space_size = font.size(' ')[0]
max_width, max_height = surface.get_size()
x, y = position
symbols_counter = 0
for line in lines:
for word in line:
w, h = get_word_length(font, word)
if w > max_width or h > max_height:
raise ValueError("Surface is too small")
if x + w >= max_width:
x = position[0]
y += h
surface.blit(font.render(word, 0, c), (x, y))
if need_cursor and symbols_counter + len(word) >= cursor_position:
# It means that cursor is in this word or after this word
cur_coord = (x + get_word_length(font,
word[:cursor_position - symbols_counter])[0], y)
draw_cursor(surface, cur_coord, h, c)
need_cursor = False
symbols_counter += len(word) + 1
x += w + space_size
y += h
x = position[0]

3
images/dependency_graph.svg

@ -9,6 +9,9 @@
id="svg9118"
inkscape:version="1.1.1 (3bf5ae0d25, 2021-09-20, custom)"
sodipodi:docname="dependency_graph.svg"
inkscape:export-filename="/home/ennucore/dev/ironforce/images/dependency_graph.png"
inkscape:export-xdpi="1121.49"
inkscape:export-ydpi="1121.49"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:xlink="http://www.w3.org/1999/xlink"

Before

Width:  |  Height:  |  Size: 7.9 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

34
images/explanation.svg

@ -24,11 +24,11 @@
inkscape:pagecheckerboard="0"
inkscape:document-units="mm"
showgrid="false"
inkscape:zoom="0.42190593"
inkscape:cx="117.32473"
inkscape:cy="387.52714"
inkscape:zoom="0.99504919"
inkscape:cx="558.26386"
inkscape:cy="397.46779"
inkscape:window-width="1920"
inkscape:window-height="1036"
inkscape:window-height="1008"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
@ -193,19 +193,19 @@
inkscape:export-ydpi="1121.49"><tspan
x="33.883114"
y="151.60315"
id="tspan44898">keys
id="tspan6402">keys
</tspan><tspan
x="33.883114"
y="159.01149"
id="tspan44900">messages
id="tspan6404">messages
</tspan><tspan
x="33.883114"
y="166.41983"
id="tspan44902">transport
id="tspan6406">transport
</tspan><tspan
x="33.883114"
y="173.82817"
id="tspan44904">tunnels</tspan></text>
id="tspan6408">tunnels</tspan></text>
<path
style="fill:none;stroke:#000000;stroke-width:0.265;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#Arrow1Lend)"
d="m 51.174695,80.963928 c 36.826182,0 59.037355,-4.807892 71.522105,-13.48959 15.03496,-10.455072 5.67718,-25.115468 43.44786,-25.115468"
@ -389,21 +389,21 @@
inkscape:export-ydpi="1121.49"><tspan
x="62.18861"
y="50.598217"
id="tspan44908"><tspan
id="tspan6412"><tspan
style="font-family:'Inconsolata LGC';-inkscape-font-specification:'Inconsolata LGC'"
id="tspan44906">Messages are collected
id="tspan6410">Messages are collected
</tspan></tspan><tspan
x="62.18861"
y="54.296866"
id="tspan44912"><tspan
id="tspan6416"><tspan
style="font-family:'Inconsolata LGC';-inkscape-font-specification:'Inconsolata LGC'"
id="tspan44910">into a queue
id="tspan6414">into a queue
</tspan></tspan><tspan
x="62.18861"
y="57.995517"
id="tspan44916"><tspan
id="tspan6420"><tspan
style="font-family:'Inconsolata LGC';-inkscape-font-specification:'Inconsolata LGC'"
id="tspan44914">in another thread</tspan></tspan></text>
id="tspan6418">in another thread</tspan></tspan></text>
<rect
style="fill:none;fill-opacity:1;stroke:#1a5fb4;stroke-width:0.264999;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0"
id="rect29127"
@ -547,7 +547,7 @@
<g
id="g39376"
transform="matrix(0.45409146,0,0,0.45409146,31.061511,94.796231)"
inkscape:export-filename="/home/ennucore/dev/ironforce/scheme.png"
inkscape:export-filename="/home/ennucore/dev/ironforce/images/degeon_logo.png"
inkscape:export-xdpi="1121.49"
inkscape:export-ydpi="1121.49">
<path
@ -645,7 +645,7 @@
cx="58.1259"
cy="167.87944"
r="39.348465"
inkscape:export-filename="/home/ennucore/dev/ironforce/scheme.png"
inkscape:export-filename="/home/ennucore/dev/ironforce/images/degeon_logo.png"
inkscape:export-xdpi="1121.49"
inkscape:export-ydpi="1121.49" />
<text
@ -654,7 +654,7 @@
x="41.161209"
y="187.81125"
id="text41324"
inkscape:export-filename="/home/ennucore/dev/ironforce/scheme.png"
inkscape:export-filename="/home/ennucore/dev/ironforce/images/degeon_logo.png"
inkscape:export-xdpi="1121.49"
inkscape:export-ydpi="1121.49"><tspan
sodipodi:role="line"

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 40 KiB

Loading…
Cancel
Save