keebie/scripts/key_props.gd

141 lines
2.4 KiB
GDScript

class_name KeyProps
enum Char {
MAIN = 0,
SHIFT,
ALT,
ALT_SHIFT,
}
enum {
KEY = 12,
LOC,
W,
H,
X,
Y,
W2,
H2,
X2,
Y2,
R,
PX,
PY,
NUB,
}
var physical_keycode: Key = KEY_NONE
var location: KeyLocation = KEY_LOCATION_UNSPECIFIED
var width: float = 1
var height: float = 1
var x: float = 0
var y: float = 0
var width2: float = 1
var height2: float = 1
var x2: float = 0
var y2: float = 0
var angle: float = -INF
var pivot_x: float = -INF
var pivot_y: float = -INF
var homing_nub: bool = false
var main_char: String
var shift_char: String
var alt_char: String
var alt_shift_char: String
func is_char() -> bool:
return OS.is_keycode_unicode(physical_keycode)
func has_secondary_rect() -> bool:
return width2 != 1 or height2 != 1 or x2 != 0 or y2 != 0
func has_angle() -> bool:
return angle != -INF
func has_pivot_x() -> bool:
return pivot_x != -INF
func has_pivot_y() -> bool:
return pivot_y != -INF
func props_from_dict(dict: Dictionary) -> KeyProps:
if dict.has(KEY):
physical_keycode = dict[KEY]
if dict.has(LOC):
location = dict[LOC]
if dict.has(W):
width = dict[W]
if dict.has(H):
height = dict[H]
if dict.has(X):
x = dict[X]
if dict.has(Y):
y = dict[Y]
if dict.has(W2):
width2 = dict[W2]
if dict.has(H2):
height2 = dict[H2]
if dict.has(X2):
x2 = dict[X2]
if dict.has(Y2):
y2 = dict[Y2]
if dict.has(R):
angle = dict[R]
if dict.has(PX):
pivot_x = dict[PX]
if dict.has(PY):
pivot_y = dict[PY]
if dict.has(NUB):
homing_nub = dict[NUB]
return self
func chars_from_dict(dict: Dictionary[Char, String], override: bool = true) -> KeyProps:
if dict.has(Char.MAIN) and (override or not main_char):
main_char = dict[Char.MAIN]
if dict.has(Char.SHIFT) and (override or not shift_char):
shift_char = dict[Char.SHIFT]
if dict.has(Char.ALT) and (override or not alt_char):
alt_char = dict[Char.ALT]
if dict.has(Char.ALT_SHIFT) and (override or not alt_shift_char):
alt_shift_char = dict[Char.ALT_SHIFT]
if main_char == shift_char:
shift_char = ""
if main_char == alt_char:
alt_char = ""
if alt_char == alt_shift_char or shift_char == alt_shift_char:
alt_shift_char = ""
return self
func chars_to_dict() -> Dictionary[Char, String]:
var dict: Dictionary[Char, String] = {}
if main_char:
dict[Char.MAIN] = main_char
if shift_char:
dict[Char.SHIFT] = shift_char
if alt_char:
dict[Char.ALT] = alt_char
if alt_shift_char:
dict[Char.ALT_SHIFT] = alt_shift_char
return dict