Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/new_game.lua

raw
1 --[[ Copyright (c) 2010 Edvin "Lego3" Linge
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --! Class for the difficulty choice window.
22 class "UINewGame" (UIResizable)
23
24 ---@type UINewGame
25 local UINewGame = _G["UINewGame"]
26
27 -- Colour definitions
28 local col = {
29 bg = Colours.PanelDefault,
30 button = Colours.PanelDefault,
31 setting = Colours.Setting,
32 setting_active = Colours.SettingActive,
33 scrollbar = Colours.Scrollbar,
34 title = Colours.Title,
35 caption = Colours.Caption,
36 textbox = Colours.Textbox,
37 }
38
39 function UINewGame:UINewGame(ui)
40 self:UIResizable(ui, 320, 220, col.bg)
41
42 local app = ui.app
43 self.esc_closes = true
44 self.resizable = false
45 self.modal_class = "main menu"
46 self.on_top = true
47 self:setDefaultPosition(0.5, 0.25)
48
49 if TheApp.using_demo_files then
50 -- We're using the demo version of TH. Load directly and activate the tutorial.
51 -- Those who use the demo files probably want that anyway.
52 self.start_tutorial = true
53 self:startGame("full")
54 self:close()
55 return
56 end
57
58 self.border_sprites = app.gfx:loadSpriteTable("Bitmap", "aux_ui", true)
59 self.start_tutorial = false
60 self.difficulty = 1
61
62
63 local avail_diff = {
64 {text = _S.new_game_window.medium, tooltip = { _S.tooltip.new_game_window.medium,
65 nil, 130 }, param = "full"}
66 }
67 if TheApp.fs:fileExists("Levels", "Easy01.SAM") then
68 table.insert(avail_diff, 1, {text = _S.new_game_window.easy, tooltip = { _S.tooltip.new_game_window.easy,
69 nil, 100 }, param = "easy"})
70 self.difficulty = 2
71 end
72 if TheApp.fs:fileExists("Levels", "Hard01.SAM") then
73 avail_diff[#avail_diff + 1] = {text = _S.new_game_window.hard, tooltip = { _S.tooltip.new_game_window.hard,
74 nil, 175 }, param = "hard"}
75 end
76 self.available_difficulties = avail_diff
77
78 self.default_button_sound = "selectx.wav"
79 -- Window parts definition
80 -- Title
81 self:addBevelPanel(80, 10, 160, 20, col.title):setLabel(_S.new_game_window.caption).lowered = true
82
83 local pname = app.config.player_name
84 self.player_name = (pname and pname:len() > 0) and pname or os.getenv("USER") or os.getenv("USERNAME") or "PLAYER"
85 self:addBevelPanel(20, 45, 140, 30, col.caption, col.bg, col.bg)
86 :setLabel(_S.new_game_window.player_name).lowered = true
87 self.name_textbox = self:addBevelPanel(165, 45, 140, 30, col.textbox, col.bg, col.bg)
88 :setTooltip(_S.tooltip.new_game_window.player_name):setAutoClip(true)
89 :makeTextbox(
90 --[[persistable:new_game_confirm_name]]function()
91 local name = self.name_textbox.text
92 if not name:find("%S") then
93 self.name_textbox:setText(self.player_name)
94 else
95 self.player_name = name
96 self:saveToConfig()
97 end
98 end,
99 --[[persistable:new_game_abort_name]]function() self.name_textbox:setText(self.player_name) end)
100 :allowedInput({"alpha", "numbers", "misc"}):characterLimit(15):setText(self.player_name)
101
102 -- Difficulty
103 self:addBevelPanel(20, 80, 140, 30, col.caption, col.bg, col.bg)
104 :setLabel(_S.new_game_window.difficulty).lowered = true
105 self:addBevelPanel(165, 80, 140, 30, col.setting, nil, nil, nil, col.setting_active):setLabel(self.available_difficulties[self.difficulty].text)
106 :makeToggleButton(0, 0, 135, 30, nil, self.dropdownDifficulty):setTooltip(_S.tooltip.new_game_window.difficulty)
107
108 -- Tutorial
109 self:addBevelPanel(20, 115, 140, 30, col.caption, col.bg, col.bg)
110 :setLabel(_S.new_game_window.tutorial).lowered = true
111 self:addBevelPanel(165, 115, 140, 30, col.setting):setLabel(_S.new_game_window.option_off)
112 :makeToggleButton(0, 0, 135, 30, nil, self.buttonTutorial):setTooltip(_S.tooltip.new_game_window.tutorial)
113
114 -- Start and Cancel
115 self:addBevelPanel(20, 165, 140, 40, col.button):setLabel(_S.new_game_window.start):makeButton(0, 0, 135, 40, nil, self.buttonStart):setTooltip(_S.tooltip.new_game_window.start)
116 self:addBevelPanel(165, 165, 140, 40, col.button):setLabel(_S.new_game_window.cancel):makeButton(0, 0, 135, 40, nil, self.buttonCancel):setTooltip(_S.tooltip.new_game_window.cancel)
117 end
118
119 function UINewGame:saveToConfig()
120 self.ui.app.config.player_name = self.player_name
121 self.ui.app:saveConfig()
122 end
123
124 function UINewGame:buttonTutorial(checked, button)
125 self.start_tutorial = checked
126 button.panel_for_sprite:setLabel(checked and _S.new_game_window.option_on or _S.new_game_window.option_off)
127 end
128
129 function UINewGame:dropdownDifficulty(activate, button)
130 if activate and #self.available_difficulties > 1 then
131 self.difficulty_dropdown = UIDropdown(self.ui, self, button,
132 self.available_difficulties, self.selectDifficulty,
133 col.setting_active, col.scrollbar)
134 self:addWindow(self.difficulty_dropdown)
135 else
136 if self.difficulty_dropdown then
137 self.difficulty_dropdown:close()
138 self.difficulty_dropdown = nil
139 end
140 end
141 end
142
143 function UINewGame:selectDifficulty(number)
144 self.difficulty = number
145 end
146
147 function UINewGame:buttonStart()
148 self.name_textbox:confirm()
149 print("starting game with difficulty " .. self.available_difficulties[self.difficulty].param)
150 self:startGame(self.available_difficulties[self.difficulty].param)
151 end
152
153 function UINewGame:startGame(difficulty)
154 if self.ui.app:loadLevel(1, difficulty, nil, nil, nil, nil,
155 _S.errors.load_level_prefix, nil) and self.ui.app.world then
156 self.ui.app.moviePlayer:playAdvanceMovie(1)
157
158 -- Initiate campaign progression. The UI above may now have changed.
159 if not TheApp.using_demo_files then
160 TheApp.world.campaign_info = "TH.campaign"
161 end
162 if self.start_tutorial then
163 TheApp.ui.start_tutorial = true
164 TheApp.ui:startTutorial()
165 end
166 end
167 end
168
169 function UINewGame:buttonCancel()
170 self.name_textbox:confirm()
171 self:close()
172 end
173
174 function UINewGame:close()
175 UIResizable.close(self)
176 self.ui:addWindow(UIMainMenu(self.ui))
177 end