Skip to content

Commit 7866e45

Browse files
committed
Add timer
* HSL background color rotating * Display time * Flip scene
1 parent cc0b324 commit 7866e45

File tree

16 files changed

+710
-252
lines changed

16 files changed

+710
-252
lines changed

CCGameFramework/CCGameFramework.vcxproj

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ xcopy /r /y /e "$(ProjectDir)script" "$(TargetDir)script"</Command>
323323
<ClCompile Include="render\Direct2DRender.cpp" />
324324
<ClCompile Include="render\Direct2DRenderTarget.cpp" />
325325
<ClCompile Include="lua_ext\UI.cpp" />
326+
<ClCompile Include="ui\window\WindowLua.cpp" />
326327
<ClCompile Include="WinMain.cpp" />
327328
<ClCompile Include="ui\gdi\Color.cpp" />
328329
<ClCompile Include="ui\gdi\Font.cpp" />
@@ -341,15 +342,32 @@ xcopy /r /y /e "$(ProjectDir)script" "$(TargetDir)script"</Command>
341342
<ClCompile Include="ui\window\WindowMsgLoop.cpp" />
342343
</ItemGroup>
343344
<ItemGroup>
344-
<None Include="script\lib\core\scene.lua" />
345-
<None Include="script\lib\ui\block.lua" />
346-
<None Include="script\lib\ui\gdibase.lua" />
347-
<None Include="script\lib\ui\gradient.lua" />
348-
<None Include="script\lib\ui\text.lua" />
349-
<None Include="script\main.lua">
345+
<Text Include="script\lib\core\scene.lua">
350346
<FileType>Text</FileType>
351-
</None>
352-
<None Include="script\scene\welcome.lua" />
347+
</Text>
348+
<Text Include="script\lib\ui\block.lua">
349+
<FileType>Text</FileType>
350+
</Text>
351+
<Text Include="script\lib\ui\gdibase.lua">
352+
<FileType>Text</FileType>
353+
</Text>
354+
<Text Include="script\lib\ui\gradient.lua">
355+
<FileType>Text</FileType>
356+
</Text>
357+
<Text Include="script\lib\ui\text.lua">
358+
<FileType>Text</FileType>
359+
</Text>
360+
<Text Include="script\main.lua">
361+
<FileType>Text</FileType>
362+
</Text>
363+
<Text Include="script\scene\welcome.lua">
364+
<FileType>Text</FileType>
365+
</Text>
366+
</ItemGroup>
367+
<ItemGroup>
368+
<Text Include="script\scene\time.lua">
369+
<FileType>Document</FileType>
370+
</Text>
353371
</ItemGroup>
354372
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
355373
<ImportGroup Label="ExtensionTargets">

CCGameFramework/CCGameFramework.vcxproj.filters

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,34 @@
326326
<ClCompile Include="lua_ext\ext.cpp">
327327
<Filter>源文件</Filter>
328328
</ClCompile>
329+
<ClCompile Include="ui\window\WindowLua.cpp">
330+
<Filter>源文件</Filter>
331+
</ClCompile>
329332
</ItemGroup>
330333
<ItemGroup>
331334
<None Include="script\main.lua">
332335
<Filter>脚本</Filter>
333336
</None>
334-
<None Include="script\scene\welcome.lua">
337+
<None Include="script\lib\ui\block.lua">
338+
<Filter>脚本</Filter>
339+
</None>
340+
<None Include="script\lib\ui\gdibase.lua">
335341
<Filter>脚本</Filter>
336342
</None>
337-
<None Include="script\lib\ui\gdibase.lua" />
338-
<None Include="script\lib\ui\block.lua" />
339-
<None Include="script\lib\core\scene.lua" />
340-
<None Include="script\lib\ui\text.lua" />
341-
<None Include="script\lib\ui\gradient.lua" />
343+
<None Include="script\lib\ui\gradient.lua">
344+
<Filter>脚本</Filter>
345+
</None>
346+
<None Include="script\lib\core\scene.lua">
347+
<Filter>脚本</Filter>
348+
</None>
349+
<None Include="script\lib\ui\text.lua">
350+
<Filter>脚本</Filter>
351+
</None>
352+
</ItemGroup>
353+
<ItemGroup>
354+
<Text Include="script\scene\welcome.lua">
355+
<Filter>脚本</Filter>
356+
</Text>
357+
<Text Include="script\scene\time.lua" />
342358
</ItemGroup>
343359
</Project>

CCGameFramework/lua_ext/UI.cpp

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ static const luaL_Reg ui_lib[] = {
1717
{ "add_obj", ui_add_obj },
1818
{ "update", ui_update_obj },
1919
{ "info", ui_info },
20+
{ "paint", ui_paint },
21+
{ "set_timer", ui_set_timer },
22+
{ "kill_timer", ui_kill_timer },
23+
{ "hsb2rgb", ui_helper_hsl2rgb },
24+
{ "rgb2hsb", ui_helper_rgb2hsl },
2025
{ nullptr, nullptr }
2126
};
2227

@@ -28,4 +33,107 @@ static int luaopen_UI(lua_State *L) {
2833
void lua_ext_register_UI(lua_State *L)
2934
{
3035
luaL_requiref(L, "UIExt", luaopen_UI, 1);
31-
}
36+
}
37+
38+
#define HSLMAX 240
39+
#define RGBMAX 255
40+
#define UNDEFINED (HSLMAX*2/3)
41+
42+
static int hue2rgb(short n1, short n2, short hue)
43+
{
44+
if (hue < 0)
45+
hue += HSLMAX;
46+
if (hue > HSLMAX)
47+
hue -= HSLMAX;
48+
49+
if (hue < (HSLMAX / 6))
50+
return (n1 + (((n2 - n1)*hue + (HSLMAX / 12)) / (HSLMAX / 6)));
51+
if (hue < (HSLMAX / 2))
52+
return (n2);
53+
if (hue < ((HSLMAX * 2) / 3))
54+
return (n1 + (((n2 - n1)*(((HSLMAX * 2) / 3) - hue) + (HSLMAX / 12)) / (HSLMAX / 6)));
55+
return (n1);
56+
}
57+
58+
int ui_helper_hsl2rgb(lua_State* L)
59+
{
60+
auto h = short(luaL_checknumber(L, 1));
61+
auto s = short(luaL_checknumber(L, 2));
62+
auto l = short(luaL_checknumber(L, 3));
63+
64+
byte r, g, b;
65+
short Magic1, Magic2;
66+
67+
if (0 == s)
68+
{
69+
r = g = b = (l*RGBMAX) / HSLMAX;
70+
}
71+
else
72+
{
73+
if (l <= (HSLMAX / 2))
74+
Magic2 = (l*(HSLMAX + s) + (HSLMAX / 2)) / HSLMAX;
75+
else
76+
Magic2 = l + s - ((l*s) + (HSLMAX / 2)) / HSLMAX;
77+
78+
Magic1 = 2 * l - Magic2;
79+
80+
r = (hue2rgb(Magic1, Magic2, h + (HSLMAX / 3))*RGBMAX + (HSLMAX / 2)) / HSLMAX;
81+
g = (hue2rgb(Magic1, Magic2, h)*RGBMAX + (HSLMAX / 2)) / HSLMAX;
82+
b = (hue2rgb(Magic1, Magic2, h - (HSLMAX / 3))*RGBMAX + (HSLMAX / 2)) / HSLMAX;
83+
}
84+
85+
CStringA str;
86+
str.AppendFormat("#%02X%02X%02X", (byte)(r * 255.0), (byte)(g * 255.0), (byte)(b * 255.0));
87+
lua_pushlstring(L, str.GetBuffer(), str.GetLength());
88+
89+
return 1;
90+
}
91+
92+
int ui_helper_rgb2hsl(lua_State* L)
93+
{
94+
auto r = byte(luaL_checknumber(L, 1));
95+
auto g = byte(luaL_checknumber(L, 2));
96+
auto b = byte(luaL_checknumber(L, 3));
97+
auto cMax = __max(r, __max(g, b));
98+
auto cMin = __min(r, __min(g, b));
99+
100+
double h, s, l;
101+
short Rdelta, Gdelta, Bdelta;
102+
103+
l = (((cMax + cMin)*HSLMAX) + RGBMAX) / (2 * RGBMAX);
104+
105+
if (cMax == cMin)
106+
{
107+
s = 0;
108+
h = UNDEFINED;
109+
}
110+
else
111+
{
112+
if (l <= (HSLMAX / 2))
113+
s = (((cMax - cMin)*HSLMAX) + ((cMax + cMin) / 2)) / (cMax + cMin);
114+
else
115+
s = (((cMax - cMin)*HSLMAX) + ((2 * RGBMAX - cMax - cMin) / 2)) / (2 * RGBMAX - cMax - cMin);
116+
117+
Rdelta = (((cMax - r)*(HSLMAX / 6)) + ((cMax - cMin) / 2)) / (cMax - cMin);
118+
Gdelta = (((cMax - g)*(HSLMAX / 6)) + ((cMax - cMin) / 2)) / (cMax - cMin);
119+
Bdelta = (((cMax - b)*(HSLMAX / 6)) + ((cMax - cMin) / 2)) / (cMax - cMin);
120+
121+
if (r == cMax)
122+
h = Bdelta - Gdelta;
123+
else if (g == cMax)
124+
h = (HSLMAX / 3) + Rdelta - Bdelta;
125+
else
126+
h = ((2 * HSLMAX) / 3) + Gdelta - Rdelta;
127+
128+
if (h < 0)
129+
h += HSLMAX;
130+
if (h > HSLMAX)
131+
h -= HSLMAX;
132+
}
133+
134+
lua_pushnumber(L, h);
135+
lua_pushnumber(L, s);
136+
lua_pushnumber(L, l);
137+
138+
return 3;
139+
}

CCGameFramework/lua_ext/UI.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ extern int ui_add_obj(lua_State *L);
88
extern int ui_update_obj(lua_State *L);
99
extern int ui_info(lua_State *L);
1010

11+
extern int ui_paint(lua_State *L);
12+
13+
extern int ui_set_timer(lua_State *L);
14+
extern int ui_kill_timer(lua_State *L);
15+
16+
extern int ui_helper_hsl2rgb(lua_State *L);
17+
extern int ui_helper_rgb2hsl(lua_State *L);
18+
1119
#endif

CCGameFramework/render/Direct2DRenderTarget.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ bool Direct2DRenderTarget::StartRendering()
6868
HRESULT Direct2DRenderTarget::StopRendering()
6969
{
7070
HRESULT result = d2dRenderTarget->EndDraw();
71-
d2dRenderTarget = nullptr;
7271
return result;
7372
}
7473

CCGameFramework/script/lib/core/scene.lua

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,42 @@ local M = {}
33
_G[modname] = M
44
package.loaded[modname] = M
55

6+
M.win_event = {
7+
created = 1,
8+
moving = 2,
9+
moved = 3,
10+
enabled = 4,
11+
disabled = 5,
12+
gotfocus = 6,
13+
lostfocus = 7,
14+
activated = 8,
15+
deactivated = 9,
16+
opened = 10,
17+
closing = 11,
18+
closed = 12,
19+
paint = 13,
20+
destroying = 14,
21+
destroyed = 15,
22+
timer = 100,
23+
leftbuttondown = 200,
24+
leftbuttonup = 201,
25+
leftbuttondoubleclick = 202,
26+
rightbuttondown = 203,
27+
rightbuttonup = 204,
28+
rightbuttondoubleclick = 205,
29+
middlebuttondown = 206,
30+
middlebuttonup = 207,
31+
middlebuttondoubleclick = 208,
32+
horizontalwheel = 209,
33+
verticalwheel = 210,
34+
mousemoving = 211,
35+
keydown = 300,
36+
keyup = 301,
37+
syskeydown = 302,
38+
syskeyup = 303,
39+
char = 304,
40+
}
41+
642
function M:new(o)
743
o = o or {layers={}}
844
setmetatable(o, self)
@@ -12,16 +48,15 @@ end
1248

1349
function M:add(o)
1450
o.handle = UIExt.add_obj(o)
15-
UIExt.update(o)
16-
return o.handle
51+
o:update()
52+
return o
1753
end
1854

19-
function M:FlipScene()
20-
if CurrentScene ~= nil then
21-
CurrentScene:destroy()
55+
function M:event(id, ...)
56+
local f = self.handler[id]
57+
if f then
58+
f(self, ...)
2259
end
23-
CurrentScene = self
24-
CurrentScene:init()
2560
end
2661

2762
return M

CCGameFramework/script/lib/ui/gdibase.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ function M:new(o)
1616
return o;
1717
end
1818

19+
function M:update()
20+
UIExt.update(self)
21+
end
22+
1923
return M

CCGameFramework/script/main.lua

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ UIExt.trace('Loading script...')
22

33
CurrentScene = nil
44

5-
local WelcomeScene = require("script.scene.welcome")
6-
local Welcome = WelcomeScene:new();
7-
Welcome:FlipScene()
5+
function PassEventToScene(id, ...)
6+
UIExt.trace('[!] Event: ' .. id)
7+
CurrentScene:event(id, ...)
8+
end
9+
10+
function FlipScene(scene)
11+
if CurrentScene then
12+
CurrentScene:event(CurrentScene.win_event.destroyed)
13+
CurrentScene:destroy()
14+
end
15+
CurrentScene = Window.Scene[scene]:new()
16+
CurrentScene:init()
17+
CurrentScene:event(CurrentScene.win_event.created)
18+
UIExt.paint()
19+
end
20+
21+
Window = {
22+
Scene = {
23+
Welcome = require("script.scene.welcome"),
24+
Time = require("script.scene.time")
25+
}
26+
}
27+
28+
FlipScene('Welcome')

0 commit comments

Comments
 (0)