You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
5.1 KiB

  1. """ Original code from https://github.com/skullydazed/kle2xy
  2. """
  3. import hjson
  4. from decimal import Decimal
  5. class KLE2xy(list):
  6. """Abstract interface for interacting with a KLE layout.
  7. """
  8. def __init__(self, layout=None, name='', invert_y=True):
  9. super(KLE2xy, self).__init__()
  10. self.name = name
  11. self.invert_y = invert_y
  12. self.key_width = Decimal('19.05')
  13. self.key_skel = {'decal': False, 'border_color': 'none', 'keycap_profile': '', 'keycap_color': 'grey', 'label_color': 'black', 'label_size': 3, 'label_style': 4, 'width': Decimal('1'), 'height': Decimal('1')}
  14. self.rows = Decimal(0)
  15. self.columns = Decimal(0)
  16. if layout:
  17. self.parse_layout(layout)
  18. @property
  19. def width(self):
  20. """Returns the width of the keyboard plate.
  21. """
  22. return (Decimal(self.columns) * self.key_width) + self.key_width / 2
  23. @property
  24. def height(self):
  25. """Returns the height of the keyboard plate.
  26. """
  27. return (self.rows * self.key_width) + self.key_width / 2
  28. @property
  29. def size(self):
  30. """Returns the size of the keyboard plate.
  31. """
  32. return (self.width, self.height)
  33. def attrs(self, properties):
  34. """Parse the keyboard properties dictionary.
  35. """
  36. # FIXME: Store more than just the keyboard name.
  37. if 'name' in properties:
  38. self.name = properties['name']
  39. def parse_layout(self, layout): # noqa FIXME(skullydazed): flake8 says this has a complexity of 25, it should be refactored.
  40. # Wrap this in a dictionary so hjson will parse KLE raw data
  41. layout = '{"layout": [' + layout + ']}'
  42. layout = hjson.loads(layout)['layout']
  43. # Initialize our state machine
  44. current_key = self.key_skel.copy()
  45. current_row = Decimal(0)
  46. current_col = Decimal(0)
  47. if isinstance(layout[0], dict):
  48. self.attrs(layout[0])
  49. layout = layout[1:]
  50. for row_num, row in enumerate(layout):
  51. self.append([])
  52. # Process the current row
  53. for key in row:
  54. if isinstance(key, dict):
  55. if 'w' in key and key['w'] != Decimal(1):
  56. current_key['width'] = Decimal(key['w'])
  57. if 'w2' in key and 'h2' in key and key['w2'] == 1.5 and key['h2'] == 1:
  58. # FIXME: ISO Key uses these params: {x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25}
  59. current_key['isoenter'] = True
  60. if 'h' in key and key['h'] != Decimal(1):
  61. current_key['height'] = Decimal(key['h'])
  62. if 'a' in key:
  63. current_key['label_style'] = self.key_skel['label_style'] = max(min(int(key['a']), 9), 0)
  64. if 'f' in key:
  65. current_key['label_size'] = self.key_skel['label_size'] = max(min(int(key['f']), 9), 1)
  66. if 'p' in key:
  67. current_key['keycap_profile'] = self.key_skel['keycap_profile'] = key['p']
  68. if 'c' in key:
  69. current_key['keycap_color'] = self.key_skel['keycap_color'] = key['c']
  70. if 't' in key:
  71. # FIXME: Need to do better validation, plus figure out how to support multiple colors
  72. if '\n' in key['t']:
  73. key['t'] = key['t'].split('\n')[0]
  74. if key['t'] == "0":
  75. key['t'] = "#000000"
  76. current_key['label_color'] = self.key_skel['label_color'] = key['t']
  77. if 'x' in key:
  78. current_col += Decimal(key['x'])
  79. if 'y' in key:
  80. current_row += Decimal(key['y'])
  81. if 'd' in key:
  82. current_key['decal'] = True
  83. else:
  84. current_key['name'] = key
  85. current_key['row'] = round(current_row, 2)
  86. current_key['column'] = round(current_col, 2)
  87. # x,y (units mm) is the center of the key
  88. x_center = current_col + current_key['width'] / 2
  89. y_center = current_row + current_key['height'] / 2
  90. current_key['x'] = x_center * self.key_width
  91. current_key['y'] = y_center * self.key_width
  92. # Tend to our row/col count
  93. current_col += current_key['width']
  94. if current_col > self.columns:
  95. self.columns = current_col
  96. # Invert the y-axis if neccesary
  97. if self.invert_y:
  98. current_key['y'] = -current_key['y']
  99. # Store this key
  100. self[-1].append(current_key)
  101. current_key = self.key_skel.copy()
  102. # Move to the next row
  103. current_col = Decimal(0)
  104. current_row += Decimal(1)
  105. if current_row > self.rows:
  106. self.rows = Decimal(current_row)