Skip to content

Commit ad44b93

Browse files
committed
Merge branch 'release/1.2.0'
2 parents cae7a37 + 5ee3845 commit ad44b93

File tree

3 files changed

+129
-17
lines changed

3 files changed

+129
-17
lines changed

Readme.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,73 @@
1-
#ScreenManager
1+
#ScreenManager
2+
3+
The ScreenManager library is a state manager at heart which allows some nifty things, like stacking multiple screens on top of each other.
4+
5+
It also offers hooks for most of LÖVE's callback functions. Based on the type of callback the calls are rerouted to either only the active screen (love.keypressed, love.quit, ...) or to all screens (love.resize, love.visible, ...).
6+
7+
## Example
8+
9+
This is a simple example of how the ScreenManager should be used (note: You will have to change the paths in the example to fit your setup).
10+
11+
```
12+
#!lua
13+
-- main.lua
14+
15+
local ScreenManager = require('lib/ScreenManager');
16+
17+
function love.load()
18+
local screens = {
19+
main = require('src/screens/MainScreen');
20+
};
21+
22+
ScreenManager.init(screens, 'main');
23+
end
24+
25+
function love.draw()
26+
ScreenManager.draw();
27+
end
28+
29+
function love.update(dt)
30+
ScreenManager.update(dt);
31+
end
32+
```
33+
Note how MainScreen inherits from Screen.lua. This isn't mandatory, but recommended since Screen.lua already has templates for most of the callback functions.
34+
35+
```
36+
#!lua
37+
-- MainScreen.lua
38+
39+
local Screen = require('lib/Screen');
40+
41+
local MainScreen = {};
42+
43+
function MainScreen.new()
44+
local self = Screen.new();
45+
46+
local x, y, w, h = 20, 20, 40, 20;
47+
48+
function self:draw()
49+
love.graphics.rectangle('fill', x, y, w, h);
50+
end
51+
52+
function self:update(dt)
53+
w = w + 2;
54+
h = h + 1;
55+
end
56+
57+
return self;
58+
end
59+
60+
return MainScreen;
61+
```
62+
63+
## License
64+
65+
Copyright (c) 2014 - 2015 Robert Machmer
66+
67+
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
68+
69+
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
70+
71+
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
72+
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
73+
3. This notice may not be removed or altered from any source distribution.

Screen.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--===============================================================================--
22
-- --
3-
-- Copyright (c) 2014 Robert Machmer --
3+
-- Copyright (c) 2014 - 2015 Robert Machmer --
44
-- --
55
-- This software is provided 'as-is', without any express or implied --
66
-- warranty. In no event will the authors be held liable for any damages --
@@ -61,6 +61,8 @@ function Screen.new()
6161

6262
function self:mousefocus(focus) end
6363

64+
function self:quit(dquit) end
65+
6466
function self:isActive()
6567
return active;
6668
end

ScreenManager.lua

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--===============================================================================--
22
-- --
3-
-- Copyright (c) 2014 Robert Machmer --
3+
-- Copyright (c) 2014 - 2015 Robert Machmer --
44
-- --
55
-- This software is provided 'as-is', without any express or implied --
66
-- warranty. In no event will the authors be held liable for any damages --
@@ -22,18 +22,26 @@
2222

2323
local ScreenManager = {};
2424

25+
-- ------------------------------------------------
26+
-- Local Variables
27+
-- ------------------------------------------------
28+
2529
local stack = {};
30+
local screens = {};
2631

2732
-- ------------------------------------------------
2833
-- Module Functions
2934
-- ------------------------------------------------
3035

3136
---
32-
-- Initialise the ScreenManager and
33-
-- @param screen
37+
-- Initialise the ScreenManager. This pushes the first
38+
-- screen to the stack.
39+
-- @param nscreens - The list of possible screens.
40+
-- @param screen - The first screen to push to the stack.
3441
--
35-
function ScreenManager.init(screen)
42+
function ScreenManager.init(nscreens, screen)
3643
stack = {};
44+
ScreenManager.setScreens(nscreens);
3745
ScreenManager.push(screen);
3846
end
3947

@@ -53,19 +61,28 @@ end
5361
-- it will overlay all the other screens.
5462
-- Screens below the this new screen will be set inactive.
5563
--
56-
-- @param nscreen
64+
-- @param screen - The name of the screen to push on the stack.
5765
--
58-
function ScreenManager.push(nscreen)
66+
function ScreenManager.push(screen)
5967
-- Deactivate the previous screen if there is one.
6068
if ScreenManager.peek() then
6169
ScreenManager.peek():setActive(false);
6270
end
6371

6472
-- Push the new screen onto the stack.
65-
stack[#stack + 1] = nscreen;
73+
if screens[screen] then
74+
stack[#stack + 1] = screens[screen].new();
75+
else
76+
local str = "{";
77+
for i, v in pairs(screens) do
78+
str = str .. i .. ', ';
79+
end
80+
str = str .. "}";
81+
error('"' .. screen .. '" is not a valid screen. You will have to add a new one to your screen list or use one of the existing screens: ' .. str);
82+
end
6683

6784
-- Create the new screen and initialise it.
68-
nscreen:init();
85+
stack[#stack]:init();
6986
end
7087

7188
---
@@ -76,7 +93,7 @@ function ScreenManager.peek()
7693
end
7794

7895
---
79-
-- Removes the topmost screen of the stack
96+
-- Removes the topmost screen of the stack.
8097
--
8198
function ScreenManager.pop()
8299
if #stack > 1 then
@@ -139,21 +156,21 @@ end
139156
---
140157
-- Update all screens on the stack whenever the game window gains or
141158
-- loses focus.
142-
-- @param dfocus
159+
-- @param nfocus
143160
--
144-
function ScreenManager.focus(dfocus)
161+
function ScreenManager.focus(nfocus)
145162
for i = 1, #stack do
146-
stack[i]:focus(dfocus);
163+
stack[i]:focus(nfocus);
147164
end
148165
end
149166

150167
---
151168
-- Update all screens on the stack whenever the game window is minimized.
152-
-- @param dvisible
169+
-- @param nvisible
153170
--
154-
function ScreenManager.visible(dvisible)
171+
function ScreenManager.visible(nvisible)
155172
for i = 1, #stack do
156-
stack[i]:visible(dvisible);
173+
stack[i]:visible(nvisible);
157174
end
158175
end
159176

@@ -211,6 +228,27 @@ function ScreenManager.mousefocus(focus)
211228
ScreenManager.peek():mousefocus(focus);
212229
end
213230

231+
---
232+
-- Reroute the quit callback to the currently active screen.
233+
-- @param dquit
234+
--
235+
function ScreenManager.quit(dquit)
236+
ScreenManager.peek():quit(dquit);
237+
end
238+
239+
-- ------------------------------------------------
240+
-- Setters
241+
-- ------------------------------------------------
242+
243+
---
244+
-- Set a new table of screens from which to pick a new screen when
245+
-- pushing / switching.
246+
-- @param nscreens
247+
--
248+
function ScreenManager.setScreens(nscreens)
249+
screens = nscreens;
250+
end
251+
214252
-- ------------------------------------------------
215253
-- Return Module
216254
-- ------------------------------------------------

0 commit comments

Comments
 (0)