I like MVC pattern to prevent my GUI scripts from becoming unmanageable too quickly.
I found great tutorials here but I lacked a functional sample in v2.
Here is my attempt. I am not very experienced and this interpretation of MVC maybe isn't canonic.
But I struggled some hours to do this and it could help you.
Code: Select all
Class App_Infos {
static name := "The Application"
}
Class Something_Model {
__New(*) {
this.button_times_clicked := 0
}
}
Class Something_View Extends Gui {
GUI_WIDTH := 400
__New(controller_arg, win_title) {
super.__New(, Title := win_title)
super.Opt("+OwnDialogs")
this.controller := controller_arg
this.nice_btn := this.Make_Nice_Button()
super.Show("w" this.GUI_WIDTH)
super.OnEvent("Close", this.Close_View)
super.OnEvent("Escape", this.Close_View)
}
Make_Nice_Button() {
control := super.Add("Button", , "I'm a nice button")
control.OnEvent("Click",this.Click_Nice_Button.Bind(this))
Return control
}
Click_Nice_Button(*) {
this.controller.Click_Nice_Button()
}
Close_View(*) {
this.controller.Close_View()
}
}
Class Something_Controller {
__New(model_arg) {
this.model := model_arg
this.view := Something_View(this, App_Infos.name)
this.is_done := false
}
Click_Nice_Button() {
this.model.button_times_clicked += 1
times := this.model.button_times_clicked
MsgBox "You clicked me " times " time(s) !"
}
Close_View() {
this.is_done := true
this.view.Destroy()
}
}
my_model := Something_Model()
my_app := Something_Controller(my_model)
While not my_app.is_done { ; Waits for the View to be closed.
}
times := my_model.button_times_clicked
MsgBox "This is done !`nThe nice button was clicked " times " time(s) !"