Commit ac847065 authored by adud's avatar adud

add conversion between coordinates and cursor position

This is the core of the workspace, and will be used in functions such as
going up/down and every mouse event
parent b60a2cb5
......@@ -5,19 +5,28 @@
.. class:: WorkSpace
A WorkSpace, to display and edit signed speech.
.. attribute:: sep(Vector2)
A WorkSpace, to display and edit signed speech.
The size of the horizontal and vertical separators between signs.
This attribute is a script variable.
.. attribute:: sep(Vector2)
.. attribute:: text(signs Array)
The signed speech, stored in one single array (like strings).
The size of the horizontal and vertical separators between signs.
This attribute is a script variable.
.. todo:: Should be accessed through functions.
.. attribute:: text(signs Array)
The signed speech, stored in one single array (like strings).
.. function:: dispose_childs()
.. todo:: Should be accessed through functions.
Draw all signs in the speech
.. method:: dispose_childs()
Draw all signs in the speech
.. method:: get_nearest_gap(pos)
get the gap between two signs which is the nearest from a position (usefull for aligning elements)
:param Vector2 pos: the position to query
:return: the index ``i`` of :attr:`text` such that the first element to the left of the gap is ``text[i]``. If
``i == len(text)``, the nearest gap is the end of the speech.
:rettype: int
......@@ -34,9 +34,45 @@ func dispose_childs():
fit_child_in_rect(symb, Rect2(pos, symb_size))
pos.x += space.x
# return true iff the nearest gap from position pos is before the sign sig
# to be used in get_nearest_gap dichotomy
func _is_before(pos, sig):
var sig_pos = sig.get_position()
var sig_size = sig.get_size()
# vertical (y) dichotomy
if pos.y < sig_pos.y - sep.y/2:
return true
if pos.y > sig_pos.y + sig_size.y + sep.y/2:
return false
# horizontal (x) dichotomy
if pos.x < sig_pos.x + sig_size.x / 2:
return true
return false
func get_nearest_gap(pos):
# simple dichotomy
var beg = -1
var end = len(text)
var mid = 0
# invariant: the gap is between text[beg] and text[end]
while end - beg > 1:
mid = (beg + end) / 2
if _is_before(pos, text[mid]):
end = mid
else:
beg = mid
return end
func test(n):
# testing the disposition of childs with a
func test_disposition(n):
var symb
var pos_in_line
......@@ -66,14 +102,34 @@ func test(n):
line_number * (symb_size + sep).y)
real_pos = text[i].get_position()
if real_pos != est_pos:
print("Workspace Test Failure, pos should be: ", est_pos,
"it is: ", real_pos)
push_error("Workspace Test Failure, pos should be: %s, it is %s"
% [est_pos, real_pos])
# test nearest gap (random)
var bound = Rect2(- sep.x - symb_size.x/2, -sep.y/2,
sep.x + symb_size.x, sep.y + symb_size.y)
var twid = Vector2(0,0)
var gap = 0
var pos = Vector2(0,0)
for i in range(len(text)):
twid = Vector2(rand_range(bound.position.x, bound.end.x),
rand_range(bound.position.y, bound.end.y))
pos = text[i].get_position() + twid
gap = get_nearest_gap(pos)
if gap != i:
push_error("Nearest Gap Error: should be %d, is %d, pos %f"
% [i, gap, pos])
else:
print(twid)
# Called when the node enters the scene tree for the first time.
func _ready():
if DEBUG:
test(20)
test_disposition(20)
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment