-
Notifications
You must be signed in to change notification settings - Fork 43
/
guiopedia.py
198 lines (160 loc) · 8.78 KB
/
guiopedia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import re
from collections import OrderedDict
from os import listdir, linesep
from os.path import isfile, join, basename, splitext
import pygame
import pygame_gui
from pygame_gui.elements import UITextBox
class GUIopediaWindow(pygame_gui.elements.UIWindow):
def __init__(self, manager):
super().__init__(pygame.Rect((200, 50), (420, 520)),
manager,
window_display_title='GUIopedia!',
object_id="#guiopedia_window")
search_bar_top_margin = 2
search_bar_bottom_margin = 2
self.search_box = pygame_gui.elements.UITextEntryLine(pygame.Rect((150,
search_bar_top_margin),
(230, 30)),
manager=manager,
container=self,
parent_element=self)
self.search_label = pygame_gui.elements.UILabel(pygame.Rect((90,
search_bar_top_margin),
(56,
self.search_box.rect.height)),
"Search:",
manager=manager,
container=self,
parent_element=self)
self.home_button = pygame_gui.elements.UIButton(pygame.Rect((20, search_bar_top_margin),
(29, 29)),
'',
manager=manager,
container=self,
parent_element=self,
object_id='#home_button')
self.remaining_window_size = (self.get_container().get_size()[0],
(self.get_container().get_size()[1] -
(self.search_box.rect.height +
search_bar_top_margin +
search_bar_bottom_margin)))
self.pages = {}
page_path = 'data/guiopedia/'
file_paths = [join(page_path, f) for f in listdir(page_path) if isfile(join(page_path, f))]
for file_path in file_paths:
with open(file_path, 'r') as page_file:
file_id = splitext(basename(file_path))[0]
file_data = ""
for line in page_file:
line = line.rstrip(linesep).lstrip()
# kind of hacky way to add back spaces at the end of new lines that
# are removed by the pyCharm HTML
# editor. perhaps our HTML parser needs to handle this case
# (turning new lines into spaces
# but removing spaces at the start of rendered lines?)
if len(line) > 0:
if line[-1] != '>':
line += ' '
file_data += line
self.pages[file_id] = file_data
index_page = self.pages['index']
self.page_y_start_pos = (self.search_box.rect.height +
search_bar_top_margin +
search_bar_bottom_margin)
self.page_display = UITextBox(index_page,
pygame.Rect((0, self.page_y_start_pos),
self.remaining_window_size),
manager=manager,
container=self,
parent_element=self)
def process_event(self, event):
handled = super().process_event(event)
if event.type == pygame_gui.UI_TEXT_BOX_LINK_CLICKED:
self.open_new_page(event.link_target)
handled = True
if (event.type == pygame_gui.UI_TEXT_ENTRY_FINISHED and
event.ui_element == self.search_box):
results = self.search_pages(event.text)
self.create_search_results_page(results)
self.open_new_page('results')
handled = True
if (event.type == pygame_gui.UI_BUTTON_PRESSED and
event.ui_object_id == '#guiopedia_window.#home_button'):
self.open_new_page('index')
handled = True
return handled
def search_pages(self, search_string: str):
results = {}
words = search_string.split()
for page in self.pages.keys():
total_occurances_of_search_words = 0
for word in words:
word_occurances = self.search_text_for_occurrences_of_word(word, self.pages[page])
total_occurances_of_search_words += word_occurances
if total_occurances_of_search_words > 0:
results[page] = total_occurances_of_search_words
sorted_results = sorted(results.items(), key=lambda item: item[1], reverse=True)
return OrderedDict(sorted_results)
@staticmethod
def search_text_for_occurrences_of_word(word_to_search_for: str, text_to_search: str) -> int:
return sum(1 for _ in re.finditer(r'\b%s\b' % re.escape(word_to_search_for),
text_to_search,
flags=re.IGNORECASE))
def open_new_page(self, page_link: str):
self.page_display.kill()
self.page_display = None
if page_link in self.pages:
text = self.pages[page_link]
self.page_display = UITextBox(text,
pygame.Rect((0,
self.page_y_start_pos),
self.remaining_window_size),
manager=self.ui_manager,
container=self,
parent_element=self)
def create_search_results_page(self, results):
results_text = '<font size=5>Search results</font>'
if len(results) == 0:
results_text += '<br><br> No Results Found.'
else:
results_text += '<br><br>' + str(len(results)) + ' results found:'
for result in results.keys():
results_text += '<br><br> - <a href=\"' + result + '\">' + result + '</a>'
self.pages['results'] = results_text
class GUIopediaApp:
def __init__(self):
pygame.init()
pygame.display.set_caption('GUIopedia App')
self.window_surface = pygame.display.set_mode((800, 600))
self.background = pygame.Surface((800, 600))
self.background.fill(pygame.Color('#707070'))
self.manager = pygame_gui.UIManager((800, 600), "data/themes/guiopedia_theme.json")
self.manager.preload_fonts([{'name': 'noto_sans', 'point_size': 24, 'style': 'bold'},
{'name': 'noto_sans', 'point_size': 24, 'style': 'bold_italic'},
{'name': 'noto_sans', 'point_size': 18, 'style': 'bold'},
{'name': 'noto_sans', 'point_size': 18, 'style': 'regular'},
{'name': 'noto_sans', 'point_size': 18, 'style': 'bold_italic'},
{'name': 'noto_sans', 'point_size': 14, 'style': 'bold'}
])
self.guiopedia_window = GUIopediaWindow(manager=self.manager)
self.clock = pygame.time.Clock()
self.is_running = True
def run(self):
while self.is_running:
time_delta = self.clock.tick(60) / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.is_running = False
if (event.type == pygame.KEYDOWN and
event.key == pygame.K_F1 and
not self.guiopedia_window.alive()):
self.guiopedia_window = GUIopediaWindow(manager=self.manager)
self.manager.process_events(event)
self.manager.update(time_delta)
self.window_surface.blit(self.background, (0, 0))
self.manager.draw_ui(self.window_surface)
pygame.display.update()
if __name__ == '__main__':
app = GUIopediaApp()
app.run()