#NoEnv
SetBatchLines, -1
; Predefined values ----------------------------------------------------------------------------------------------------------------
GuiTitle := "Game Info Editor" ; main Gui title
FileDir := A_ScriptDir . "\CSVFILES" ; path of the folder containing the files
Separator := ">" ; field separator
NumOfFields := 10 ; number of fields in the game records
FieldNames := ["Rom Name" ; array of field names for the game record, adjust them to your needs
, "Game Name"
, "Year"
, "Rating"
, "Publisher"
, "Developer"
, "Genre"
, "Score"
, "Number Of Players"
, "Description"]
FieldOptions := ["Disabled" ; special Gui options for the edit fields, adjust them to your needs
, ""
, ""
, ""
, ""
, ""
, ""
, ""
, ""
, "r10"
, ""
, ""]
LVW := 200 ; width of the ListView controls
EDW := 600 ; width of the Edit controls
; Get the height of a single-line edit control -------------------------------------------------------------------------------------
Gui, Add, Edit, vDummy, Dummy
GuiControlGet, Dummy, Pos
Gui, Destroy
EDH := DummyH
; GUI ------------------------------------------------------------------------------------------------------------------------------
Gui, Margin, 20, 10
; Create and populate the FileLV
Gui, Add, ListView, w%LVW% r10 Sort -Multi vFileLV gSubFileLV, File Name
Loop, Files, %FileDir%\*.csv
LV_Add("", A_LoopFileName)
; Create the GameLV
Gui, Add, ListView, w%LVW% r10 Sort -Multi vGameLV gSubGameLV AltSubmit, % FieldNames[1]
; Add the text and edit fields for the game records
TXY := "ym"
Loop, %NumOfFields% {
Gui, Add, Text, %TXY% h%EDH% +0x200, % FieldNames[A_Index]
Options := FieldOptions[A_Index] ; special field options
Gui, Add, Edit, xp y+0 w%EDW% %Options% vED%A_Index% gSubEditChanged +ReadOnly
TXY := "y+2"
}
; Add the action buttons
BC := 3 ; button count
BW := 100 ; button width
DX := (EDW - (BW * BC)) // (BC - 1) ; space between butons
Gui, Add, Button, w%BW% vEditBtn gSubEditBtn, Edit
Gui, Add, Button, x+%DX% yp wp Disabled vSaveBtn gSubSaveBtn, Save
Gui, Add, Button, x+%DX% yp wp Disabled vCancelBtn gSubCancelBtn, Cancel
; Adjust the height of GameLV
GuiControlGet, L, Pos, GameLV
GuiControlGet, B, Pos, CancelBtn
LVH := BH + BY - LY
GuiControl, Move, GameLV, h%LVH%
; Show the GUI
Gui, Show, , %GuiTitle%
Editing := False
Return
GuiClose:
ExitApp
; File LV label --------------------------------------------------------------------------------------------------------------------
SubFileLV:
Gui, ListView, FileLV ; set the default ListView
If (A_GuiEvent = "DoubleClick") && (A_EventInfo) && (LV_GetNext() = A_EventInfo) { ; double-click on an item
LV_GetText(CurrentFile, A_EventInfo)
Gui, ListView, GameLV ; change the default ListView
LV_Delete()
GuiControl, -ReDraw, GameLV ; stop redrawing
FileRead, CurrentContent, %FileDir%\%CurrentFile%
GameArray := [] ; initialize a real array to hold the lines
Loop, Parse, CurrentContent, `n, `r
{
If (A_LoopField) {
Fields := StrSplit(A_LoopField, Separator) ; creates a real array of fields (AHK arrays start with index 1)
GameID := Fields[1] ; use the first field as the unique ID for the game
LV_Add("", GameID) ; add GameID
GameArray[GameID] := Fields ; use GameID as key for GameArray
}
}
Loop, 2 ; adjust the column width
LV_ModifyCol(A_Index, "AutoHdr")
GuiControl, +Redraw, GameLV ; redraw the ListView
GuiControl, Focus, GameLV
LV_Modify(1, "Select")
}
Return
; Game LV label --------------------------------------------------------------------------------------------------------------------
SubGameLV:
Gui, ListView, GameLV ; set the default ListView
If (A_GuiEvent == "I") && Instr(ErrorLevel, "S", 1) { ; a new item has been selected
LV_GetText(CurrentGame, A_EventInfo)
CurrentFields := GameArray[CurrentGame] ; retrieve the fields of the selected game from GameArray
Loop, %NumOfFields% ; update the edit fields
GuiControl, , ED%A_Index%, % CurrentFields[A_Index]
}
Return
; Edit button label ----------------------------------------------------------------------------------------------------------------
SubEditBtn:
GuiControl, Disable, FileLV
GuiControl, Disable, GameLV
GuiControl, Disable, EditBtn
Loop, %NumOfFields%
GuiControl, -ReadOnly, ED%A_Index%
GuiControl, Enable, CancelBtn
GuiControl, Focus, ED2
Modified := False
Editing := True
Return
; Save button label ----------------------------------------------------------------------------------------------------------------
SubSaveBtn:
Gui, +OwnDialogs
Editing := False
MsgBox, 4, %GuiTitle%, Save the changes to file %CurrentFile%?
IfMsgBox, Yes
{
Loop, %NumOfFields% { ; update the game record
GuiControlGet, Value, , ED%A_Index%
CurrentFields[A_Index] := Value
}
; To-do: save the file!
}
; Fall through to SubCancelBtn to do the rest
; Cancel button label --------------------------------------------------------------------------------------------------------------
SubCancelBtn:
Editing := False
GuiControl, Disable, CancelBtn
GuiControl, Disable, SaveBtn
Loop, %NumOfFields%
GuiControl, +ReadOnly, ED%A_Index%
GuiControl, Enable, EditBtn
GuiControl, Enable, GameLV
GuiControl, Enable, FileLV
GuiControl, Focus, GameLV
Gui, ListView, GameLV
LV_Modify(LV_GetNext(), "Select") ; refill the game record fields
Return
; Edits label ----------------------------------------------------------------------------------------------------------------------
SubEditChanged:
If !(Editing) ; not in edit mode
Return
If !(Modified) ; first call while editing
GuiControl, Enable, SaveBtn
Modified := True ; something in some of the edit control has been changed
Return
; ----------------------------------------------------------------------------------------------------------------------------------
CtlColorStatic(W, L) { ; changes the background of ReadOnly and disabled edits to white for better readability
Static Init := OnMessage(0x0138, "CtlColorStatic") ; WM_CTLCOLORSTATIC = 0x0138
Static DCBrush := DllCall("GetStockObject", "UInt", 18, "UPtr") ; DC_BRUSH = 18
GuiControlGet, ControlName, Name, %L%
If (SubStr(ControlName, 1, 2) = "ED") {
DllCall("SetDCBrushColor", "Ptr", W, "UInt", 0xFFFFFF)
Return DCBrush
}
}