Skip to content

Commit ddc95ce

Browse files
committed
Set up user settings reader and writer
1 parent 81fb734 commit ddc95ce

File tree

4 files changed

+123
-5
lines changed

4 files changed

+123
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
*.code-workspace
99
.vscode/
1010

11+
BenderUserSettings.ini
1112
!TestImage.png
1213
!InputTemplate.png

Bender.bmx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,29 @@ Import "Types/GraphicsOutput.bmx"
1717

1818
AppTitle = "CCCP Bender 1.3.0"
1919

20+
Global g_SettingsManager:SettingsManager = Null
2021
Global g_UserInterface:UserInterface = Null
2122
Global g_FileIO:FileIO = Null
2223
Global g_GraphicsOutput:GraphicsOutput = Null
2324

2425
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2526

2627
Repeat
28+
g_SettingsManager = New SettingsManager()
2729
g_UserInterface = New UserInterface(g_DefaultInputZoom, g_DefaultOutputZoom, g_DefaultFrameCount, g_DefaultBackgroundRed, g_DefaultBackgroundGreen, g_DefaultBackgroundBlue)
2830
g_FileIO = New FileIO()
2931
g_GraphicsOutput = New GraphicsOutput(g_UserInterface.GetMaxWorkspaceWidth())
3032
EnablePolledInput()
3133

34+
'Set user settings/defaults
35+
g_UserInterface.SetInputZoomTextboxValue(g_GraphicsOutput.SetInputZoom(g_DefaultInputZoom))
36+
g_UserInterface.SetOutputZoomTextboxValue(g_GraphicsOutput.SetOutputZoom(g_DefaultOutputZoom))
37+
g_UserInterface.SetFramesTextboxValue(g_GraphicsOutput.SetFrameCount(g_DefaultFrameCount))
38+
g_UserInterface.SetColorTextboxValues(g_GraphicsOutput.SetBackgroundColor(Int[][g_DefaultBackgroundRed, g_DefaultBackgroundGreen, g_DefaultBackgroundBlue]))
39+
g_GraphicsOutput.SetDrawOutputFrameBounds(g_FileIO.SetSaveAsFrames(g_DefaultSaveAsFrames))
40+
g_UserInterface.SetFileTypeComboBoxVisible(g_FileIO.SetSaveAsIndexed(g_DefaultSaveAsIndexed))
41+
g_FileIO.SetIndexedFileType(g_DefaultIndexedFileType)
42+
3243
Repeat
3344
PollEvent()
3445

@@ -45,6 +56,8 @@ Repeat
4556
g_GraphicsOutput.LoadFile(g_FileIO.SetFileToLoad(EventExtra().ToString()))
4657
Case EVENT_MENUACTION
4758
Select EventData()
59+
Case g_UserInterface.c_SaveSettingsMenuTag
60+
g_SettingsManager.WriteSettingsFile(g_UserInterface.GetSettingsValuesForSaving())
4861
Case g_UserInterface.c_HelpMenuTag
4962
Notify(g_UserInterface.m_HelpMenuText, False)
5063
Case g_UserInterface.c_AboutMenuTag

Types/SettingsManager.bmx

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,96 @@
1+
Import "Utility.bmx"
2+
13
'//// SETTINGS MANAGER //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24

3-
Global g_DefaultInputZoom:Int = 4
4-
Global g_DefaultOutputZoom:Int = 1
5-
Global g_DefaultFrameCount:Int = 7
65
Global g_DefaultBackgroundRed:Int = 50
76
Global g_DefaultBackgroundGreen:Int = 170
87
Global g_DefaultBackgroundBlue:Int = 255
8+
Global g_DefaultInputZoom:Int = 4
9+
Global g_DefaultOutputZoom:Int = 1
10+
Global g_DefaultFrameCount:Int = 7
11+
Global g_DefaultSaveAsFrames:Int = False
12+
Global g_DefaultSaveAsIndexed:Int = False
913
Global g_DefaultIndexedFileType:String = "png"
1014

1115
Type SettingsManager
16+
17+
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18+
19+
Method New()
20+
ReadSettingsFile()
21+
EndMethod
22+
23+
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24+
25+
Method ReadSettingsFile()
26+
Local inputStream:TStream = ReadStream("BenderUserSettings.ini")
27+
If Not inputStream Then
28+
'Notify("No settings file found, using default settings!")
29+
Return
30+
EndIf
31+
32+
While Not inputStream.Eof()
33+
Local line:String = ReadLine(inputStream)
34+
35+
'Skip whitespace and comment lines
36+
If line.Length = 0 Or line.Find("//") = 0 Then
37+
Continue
38+
EndIf
39+
40+
Local propAndValue:String[] = line.Split("=")
41+
propAndValue[0] = propAndValue[0].Trim()
42+
propAndValue[1] = propAndValue[1].Trim()
43+
44+
Select propAndValue[0]
45+
Case "BackgroundRed"
46+
g_DefaultBackgroundRed = Utility.Clamp(propAndValue[1].ToInt(), 0, 255)
47+
Case "BackgroundGreen"
48+
g_DefaultBackgroundGreen = Utility.Clamp(propAndValue[1].ToInt(), 0, 255)
49+
Case "BackgroundBlue"
50+
g_DefaultBackgroundBlue = Utility.Clamp(propAndValue[1].ToInt(), 0, 255)
51+
Case "InputZoom"
52+
g_DefaultInputZoom = Utility.Clamp(propAndValue[1].ToInt(), 1, DesktopWidth() - 260)
53+
Case "OutputZoom"
54+
g_DefaultOutputZoom = Utility.Clamp(propAndValue[1].ToInt(), 1, 5)
55+
Case "FrameCount"
56+
g_DefaultFrameCount = Utility.Clamp(propAndValue[1].ToInt(), 1, 20)
57+
Case "SaveAsFrames"
58+
g_DefaultSaveAsFrames = Utility.Clamp(propAndValue[1].ToInt(), False, True)
59+
Case "SaveAsIndexed"
60+
g_DefaultSaveAsIndexed = Utility.Clamp(propAndValue[1].ToInt(), False, True)
61+
Case "IndexedFileType"
62+
propAndValue[1] = propAndValue[1].ToLower()
63+
If propAndValue[1] = "png" Or propAndValue[1] = "bmp" Then
64+
g_DefaultIndexedFileType = propAndValue[1]
65+
Else
66+
Notify("Invalid indexed file type set from settings, defaulting to PNG!")
67+
EndIf
68+
EndSelect
69+
EndWhile
70+
71+
CloseStream(inputStream)
72+
EndMethod
73+
74+
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75+
76+
Method WriteSettingsFile(propertyValues:String[])
77+
Local outputString:TStringBuilder = New TStringBuilder("// User Settings~n~n")
78+
79+
outputString.Append("BackgroundRed = " + propertyValues[0] + "~n")
80+
outputString.Append("BackgroundGreen = " + propertyValues[1] + "~n")
81+
outputString.Append("BackgroundBlue = " + propertyValues[2] + "~n")
82+
outputString.Append("InputZoom = " + propertyValues[3] + "~n")
83+
outputString.Append("OutputZoom = " + propertyValues[4] + "~n")
84+
outputString.Append("FrameCount = " + propertyValues[5] + "~n")
85+
outputString.Append("SaveAsFrames = " + propertyValues[6] + "~n")
86+
outputString.Append("SaveAsIndexed = " + propertyValues[7] + "~n")
87+
outputString.Append("IndexedFileType = " + propertyValues[8].ToLower())
88+
89+
Local saveResult:Int = SaveText(outputString.ToString(), "BenderUserSettings.ini")
90+
If saveResult Then
91+
Notify("Settings file saved successfully!")
92+
Else
93+
Notify("Failed to save settings file!", True)
94+
EndIf
95+
EndMethod
1296
EndType

Types/UserInterface.bmx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ Type UserInterface
1717
Field m_CanvasGraphicsSize:SVec2I = New SVec2I(768, 480)
1818

1919
'Title bar buttons
20+
Field m_SaveSettingsMenu:TGadget = Null
21+
Const c_SaveSettingsMenuTag:Int = 100
22+
2023
Field m_HelpMenu:TGadget = Null
2124
Field m_HelpMenuText:String = LoadText("Incbin::Assets/TextHelp")
22-
Const c_HelpMenuTag:Int = 100
25+
Const c_HelpMenuTag:Int = 101
2326

2427
Field m_AboutMenu:TGadget = Null
2528
Field m_AboutMenuText:String = LoadText("Incbin::Assets/TextAbout")
26-
Const c_AboutMenuTag:Int = 101
29+
Const c_AboutMenuTag:Int = 102
2730

2831
Field m_ButtonPanel:TGadget = Null
2932
Field m_ButtonPanelAnchor:SVec2I = New SVec2I(10, 5)
@@ -86,6 +89,7 @@ Type UserInterface
8689
m_MainWindow = CreateWindow(AppTitle, (DesktopWidth() / 2) - ((m_CanvasGraphicsAnchor[0] + m_CanvasGraphicsSize[0]) / 2), (DesktopHeight() / 2) - (m_CanvasGraphicsSize[1] / 2), m_CanvasGraphicsAnchor[0] + m_CanvasGraphicsSize[0], m_CanvasGraphicsSize[1], Null, WINDOW_TITLEBAR | WINDOW_MENU | WINDOW_RESIZABLE | WINDOW_CLIENTCOORDS | WINDOW_ACCEPTFILES)
8790
SetMinWindowSize(m_MainWindow, m_LeftColumnSize[0] + m_CanvasGraphicsSize[0], m_CanvasGraphicsSize[1])
8891

92+
m_SaveSettingsMenu = CreateMenu("Save Settings", c_SaveSettingsMenuTag, WindowMenu(m_MainWindow))
8993
m_HelpMenu = CreateMenu("Help", c_HelpMenuTag, WindowMenu(m_MainWindow))
9094
m_AboutMenu = CreateMenu("About", c_AboutMenuTag, WindowMenu(m_MainWindow))
9195
UpdateWindowMenu(m_MainWindow)
@@ -312,4 +316,20 @@ Type UserInterface
312316
HideGadget(m_SettingsIndexedFileTypeComboBox)
313317
EndIf
314318
EndMethod
319+
320+
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
321+
322+
Method GetSettingsValuesForSaving:String[]()
323+
Return [ .. 'Line continuation
324+
GadgetText(m_SettingsColorRTextbox), ..
325+
GadgetText(m_SettingsColorGTextbox), ..
326+
GadgetText(m_SettingsColorBTextbox), ..
327+
GadgetText(m_SettingsInputZoomTextbox), ..
328+
GadgetText(m_SettingsOutputZoomTextbox), ..
329+
GadgetText(m_SettingsFramesTextbox), ..
330+
String.FromInt(ButtonState(m_SettingsSaveAsFramesCheckbox)), ..
331+
String.FromInt(ButtonState(m_SettingsIndexedCheckbox)), ..
332+
GadgetText(m_SettingsIndexedFileTypeComboBox) ..
333+
]
334+
EndMethod
315335
EndType

0 commit comments

Comments
 (0)