Skip to content

Commit

Permalink
add the confirm adn init setting
Browse files Browse the repository at this point in the history
  • Loading branch information
danghoangnhan committed Oct 3, 2023
1 parent 6a52919 commit 8cd156e
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 59 deletions.
32 changes: 32 additions & 0 deletions component/modal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from component.text import TextElement
from component.button import ButtonElement
import psychopy.visual.line
from psychopy.visual.rect import Rect
from config.constant import color_dict

class ConfirmationModal:
def __init__(self, win, text,confirm_action=None,cancel_action=None):
self.win = win
self.text = TextElement(win, text, pos=(0, 0), color=color_dict["white"],)
self.confirm_button = ButtonElement(win, "Confirm", pos=(-0.3, -0.5), width=0.3, height=0.2, color="green", action=confirm_action)
self.cancel_button = ButtonElement(win, "Cancel", pos=(0.3, -0.5), width=0.3, height=0.2, color="red", action=cancel_action)
self.background_rect = Rect(win, width=1.2, height=0.9, fillColor=(0, 0, 0, 1.0), pos=(0, -0.2))
self.buttons = [self.confirm_button, self.cancel_button]
self.visible = False

def draw(self):
if self.visible:
self.background_rect.draw()
self.text.draw()
for element in self.buttons:
element.draw()

def containMouse(self, mouse):
if(self.visible)==False:
return False
return self.confirm_button.containMouse(mouse) or self.cancel_button.containMouse(mouse)

def action(self, mouse):
for button_element in self.buttons:
if button_element.containMouse(mouse):
return button_element.action(mouse)
63 changes: 47 additions & 16 deletions component/screen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from component.button import ButtonElement
from component.modal import ConfirmationModal
from component.model import Answer
from component.text import TextElement
from component.button import ButtonElement,ButtonList
Expand All @@ -8,7 +9,7 @@
from config.constant import color_dict
from config.constant import pofomopo_consonants,similarity_list
from config.dir import audio_dir
from storage.localStorage import csvHandler
from storage.localStorage import dataHandaler
import pandas as pd
import random

Expand Down Expand Up @@ -37,30 +38,34 @@ def __init__(self, win,result):
self.add_element(ButtonElement(win, "Replay", pos=(0, -0.2), width=0.3, height=0.2, color="green", action=self.replay))
self.add_element(ButtonElement(win, "Quit", pos=(0, -0.5), width=0.3, height=0.2, color="red", action=self.quit))

def replay(self):
def replay(self,mouse):
return StartScreen(self.win)

def quit(self):
def quit(self,mouse):
return None # This will end the experiment
def saveResult(self,history):
user_df = csvHandler.get_user()
user_df = dataHandaler.get_user()
user_df = user_df.drop(user_df.index)
new_history_value = pd.DataFrame([element.to_dict() for element in history])
new_history_value['participate_number'] = csvHandler.get_new_sessionId()
new_history_value['participate_number'] = dataHandaler.get_new_sessionId()
new_history_value = new_history_value.sort_values(by='question')
user_df = pd.concat([user_df, pd.DataFrame([{"participantID":csvHandler.get_new_sessionId()}])], ignore_index=True)
csvHandler.append_history_data(new_history_value)
csvHandler.append_user_data(user_df)
user_df = pd.concat([user_df, pd.DataFrame([{"participantID":dataHandaler.get_new_sessionId()}])], ignore_index=True)
dataHandaler.append_history_data(new_history_value)
dataHandaler.append_user_data(user_df)


# StartScreen
class StartScreen(BaseScreen):
def __init__(self, win):
super().__init__(win)
self.add_element(TextElement(win, "Quizz Experiment", pos=(0, 0),color=color_dict["black"]))
self.add_element(ButtonElement(win, "Start", pos=(0, -0.2), width=0.3, height=0.2, color="green", action=self.next_screen))
self.add_element(ButtonElement(win, "Start", pos=(0, -0.2), width=0.3, height=0.2, color="green", action=self.navigate_start_screen))
self.add_element(ButtonElement(win, "Setting", pos=(0, -0.5), width=0.3, height=0.2, color="blue", action=self.navigate_setting_screen))

def next_screen(self):
def navigate_setting_screen(self,mouse):
return SettingScreen(self.win)

def navigate_start_screen(self,mouse):
return TestScreen(self.win)

# TestScreen
Expand Down Expand Up @@ -109,26 +114,26 @@ def __init__(self, win):
self.update_button_states()


def next_question(self):
def next_question(self, mouse):
self.current_index += 1
self.update_button_states()
self.updateProcess()
return self

def previous_question(self):
def previous_question(self, mouse):
self.current_index -= 1
self.update_button_states()
self.updateProcess()
return self

def play_sound(self):
def play_sound(self, mouse):
current_question = self.answerList[self.current_index]
play(current_question.get_question())
return self


def loadQuestion(self, audio_dir):
question_df = csvHandler.get_exam()
question_df = dataHandaler.get_exam()
quetionList = []
for index, row in question_df.iterrows():
filename = row['path']
Expand All @@ -155,7 +160,7 @@ def update_button_states(self):
self.bofomo_consonants_list.updateState(current_question.get_answer())
self.similarities_list.updateState(current_question.get_similarity())

def submit_test(self):
def submit_test(self, mouse):
for question in self.answerList:
question.set_id(question.get_id()+1)
question.set_answer(pofomopo_consonants[question.get_answer()] if question.get_answer()!= -1 else -1)
Expand All @@ -177,4 +182,30 @@ def similarity_action(self, button_index):
current_question = self.answerList[self.current_index]
current_question.set_similarity(button_index)
self.similarities_list.updateState(button_index)
return self
return self


# Create the SettingScreen class with the Reset button and modal logic
class SettingScreen(BaseScreen):
def __init__(self, win):
super().__init__(win)
self.add_element(TextElement(win, "Settings", pos=(0, 0), color=color_dict["black"]))
self.add_element(ButtonElement(win, "Reset", pos=(0, -0.2), width=0.3, height=0.2, color="red",action=self.show_confirmation_modal))
self.add_element(ButtonElement(win, "Back", pos=(0.7, -0.8), width=0.3, height=0.2, color="grey",action=self.navigate_main_screen))
self.confirmation_modal = ConfirmationModal(win, "Are you sure you want to reset?",confirm_action=self.confirm,cancel_action=self.cancel)
self.add_element(self.confirmation_modal)

def show_confirmation_modal(self, mouse):
self.confirmation_modal.visible = True
return self

def confirm(self, mouse):
self.confirmation_modal.visible = False
dataHandaler.resetHistory()
return self

def cancel(self, mouse):
self.confirmation_modal.visible = False
return self
def navigate_main_screen(self,mouse):
return StartScreen(self.win)
14 changes: 5 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import logging
import psychopy.visual.line

from mouseHandler import is_instance_of_class



class Game:
Expand All @@ -20,17 +22,11 @@ def start(self):
# Check for mouse clicks
if self.mouse.getPressed()[0]: # 0 represents the left mouse button
for element in self.current_screen.elements:
if isinstance(element, ButtonElement) and element.containMouse(self.mouse):
if element.action:
self.setScreen(element.action())
core.wait(0.2)
if isinstance(element, ButtonList) and element.containMouse(self.mouse):
if is_instance_of_class(element) and element.containMouse(self.mouse):
if element.action:
self.setScreen(element.action(mouse=self.mouse))
self.setScreen(element.action(self.mouse))
core.wait(0.2)

if self.current_screen==None:
print("screen is none")
print("screen is None")
self.win.close()
core.quit()

Expand Down
20 changes: 20 additions & 0 deletions mouseHandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from component.button import ButtonElement, ButtonList
from component.modal import ConfirmationModal

clickable_list:list = [ButtonList,ButtonElement, ConfirmationModal]

def is_instance_of_class(variable, class_list=clickable_list):
"""
Check if a variable is an instance of any class in the class_list.
Args:
variable: The variable to be checked.
class_list: A list of class names or classes.
Returns:
True if the variable is an instance of any class in the class_list, False otherwise.
"""
for cls in class_list:
if isinstance(variable, cls):
return True
return False
83 changes: 49 additions & 34 deletions storage/localStorage.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
import pandas
from config.dir import user_dir,history_dir,exam_dir

class csvHandler:

@staticmethod
def get_new_sessionId():
user_df = pandas.read_excel(user_dir)
class xlsxHandler:

def __init__(self,dir):
self.dir = dir

def readData(self):
return pandas.read_excel(self.dir)

def readStructure(self):
return pandas.read_excel(self.dir, nrows=1)

def dumpd_data(self):
history_dir.to_excel(self.dir, index=False)

def truncate(self):
df = pandas.read_excel(self.dir)
df = pandas.DataFrame(columns=df.columns)
df.to_excel(self.dir, index=False)

def append_data(self,new_df):
df_excel = pandas.read_excel(self.dir)
result = pandas.concat([df_excel, new_df], ignore_index=True)
print(result)
result.to_excel(self.dir, index=False)


class DataHandaler:
def __init__(self):
self.user_data_handaler =xlsxHandler(dir=user_dir)
self.history_data_handaler =xlsxHandler(dir=history_dir)
self.exam_data_handler = xlsxHandler(dir=exam_dir)

def get_new_sessionId(self):
user_df = self.user_data_handaler.readData()
if user_df.empty:
return 1
max_value = user_df['participantID'].max()
return int(max_value) + 1

@staticmethod
def get_exam():
def get_exam(self):
return pandas.read_excel(exam_dir)

@staticmethod
def get_user():
return pandas.read_excel(user_dir)
def get_user(self):
return self.user_data_handaler.readData()

@staticmethod
def append_history_data(new_df):
csvHandler.append_data(new_df,history_dir)

@staticmethod
def append_user_data(new_df):
csvHandler.append_data(new_df,user_dir)
def append_history_data(self,new_df):
self.history_data_handaler.append_data(new_df=new_df)

@staticmethod
def append_data(new_df,path,sheet_name='Sheet1'):
df_excel = pandas.read_excel(path)
result = pandas.concat([df_excel, new_df], ignore_index=True)
result.to_excel(path, index=False)

@staticmethod
def dumpd_data():
history_df = pandas.read_excel(history_dir)
history_dir.to_excel('history.xlsx', index=False)

@staticmethod
def readStructure_user():
return csvHandler.readStructure(user_dir)
def append_user_data(self,new_df):
self.user_data_handaler.append_data(new_df=new_df)

def readStructure_user(self):
return self.user_data_handaler.readStructure()

def resetHistory(self):
self.user_data_handaler.truncate()
self.history_data_handaler.truncate()

@staticmethod
def readStructure(dir):
return pandas.read_excel(dir, nrows=1)
dataHandaler= DataHandaler()

0 comments on commit 8cd156e

Please sign in to comment.