Skip to content
This repository was archived by the owner on Nov 16, 2020. It is now read-only.

Commit b3f2347

Browse files
authored
love2d - ios version
1 parent b7f54e7 commit b3f2347

File tree

10 files changed

+473
-0
lines changed

10 files changed

+473
-0
lines changed

dash_love2d_ios/chart.lua

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
require "color"
2+
Object = require "classic"
3+
4+
Chart = Object:extend()
5+
6+
function Chart:new ( x, y, title, unit, value, displayColor)
7+
8+
self.start_x = x or 0
9+
self.start_y = y or 0
10+
self.title = title
11+
self.unit = unit
12+
self.value = value or {0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0} -- 20 elements
13+
self.displayColor = displayColor
14+
15+
self.chart_height = 100
16+
self.chart_width = 200
17+
18+
self.minValue = 0
19+
self.maxValue = 100
20+
self.interval = 10
21+
22+
end
23+
24+
function Chart:setValue(newValue)
25+
self.value = newValue
26+
end
27+
28+
function Chart:getValue()
29+
return self.value
30+
end
31+
32+
function Chart:setXY(x, y)
33+
self.start_x = x
34+
self.start_y = y
35+
end
36+
37+
function Chart:draw ()
38+
39+
love.graphics.setNewFont(14)
40+
love.graphics.setColor(self.displayColor)
41+
love.graphics.print(self.title .. " " .. self.value[ #self.value ] .." "..self.unit, self.start_x+35, self.start_y)
42+
43+
local chart_x = self.start_x
44+
local chart_y = self.start_y + 10
45+
46+
love.graphics.setColor(gray2Color)
47+
love.graphics.setNewFont(10)
48+
love.graphics.print("100", self.start_x+10, chart_y)
49+
love.graphics.print("75", self.start_x+10, chart_y + self.chart_height/4)
50+
love.graphics.print("50", self.start_x+10, chart_y + 2*self.chart_height/4)
51+
love.graphics.print("25", self.start_x+10, chart_y + 3*self.chart_height/4)
52+
love.graphics.line( chart_x, chart_y + self.chart_height/4, chart_x + self.chart_width, chart_y + self.chart_height/4)
53+
love.graphics.line( chart_x, chart_y + 2*self.chart_height/4, chart_x + self.chart_width, chart_y + 2*self.chart_height/4)
54+
love.graphics.line( chart_x, chart_y + 3*self.chart_height/4, chart_x + self.chart_width, chart_y + 3*self.chart_height/4)
55+
56+
-- display time-series values
57+
local preX = 0
58+
local preY = 0
59+
local curX = 0
60+
local curY = 0
61+
love.graphics.setColor(self.displayColor)
62+
for key, value in ipairs(self.value)
63+
do
64+
curX = (key * self.interval) + chart_x - self.interval
65+
curY = chart_y + self.chart_height - self.value[key]
66+
if key == 1 then -- first element. Same as curX, curY
67+
preX = curX
68+
preY = curY
69+
else
70+
preX = (key-1) * self.interval + chart_x - self.interval
71+
preY = chart_y + self.chart_height - self.value[key-1]
72+
end
73+
love.graphics.circle("fill", curX, curY, 2)
74+
love.graphics.line( preX, preY, curX, curY)
75+
end
76+
77+
love.graphics.setColor(whiteColor)
78+
love.graphics.line( chart_x, chart_y , chart_x, chart_y + self.chart_height) -- vertical
79+
love.graphics.line( chart_x, chart_y + self.chart_height, chart_x + self.chart_width, chart_y + self.chart_height) -- horizontal
80+
81+
end

dash_love2d_ios/classic.lua

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--
2+
-- classic
3+
--
4+
-- Copyright (c) 2014, rxi
5+
--
6+
-- This module is free software; you can redistribute it and/or modify it under
7+
-- the terms of the MIT license. See LICENSE for details.
8+
--
9+
10+
11+
local Object = {}
12+
Object.__index = Object
13+
14+
15+
function Object:new()
16+
end
17+
18+
19+
function Object:extend()
20+
local cls = {}
21+
for k, v in pairs(self) do
22+
if k:find("__") == 1 then
23+
cls[k] = v
24+
end
25+
end
26+
cls.__index = cls
27+
cls.super = self
28+
setmetatable(cls, self)
29+
return cls
30+
end
31+
32+
33+
function Object:implement(...)
34+
for _, cls in pairs({...}) do
35+
for k, v in pairs(cls) do
36+
if self[k] == nil and type(v) == "function" then
37+
self[k] = v
38+
end
39+
end
40+
end
41+
end
42+
43+
44+
function Object:is(T)
45+
local mt = getmetatable(self)
46+
while mt do
47+
if mt == T then
48+
return true
49+
end
50+
mt = getmetatable(mt)
51+
end
52+
return false
53+
end
54+
55+
56+
function Object:__tostring()
57+
return "Object"
58+
end
59+
60+
61+
function Object:__call(...)
62+
local obj = setmetatable({}, self)
63+
obj:new(...)
64+
return obj
65+
end
66+
67+
68+
return Object

dash_love2d_ios/color.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
blackColor = { 0, 0, 0, 1 }
3+
whiteColor = { 1, 1, 1, 1 }
4+
redColor = { 1, 0, 0, 1 }
5+
greenColor = { 0, 1, 0, 1 }
6+
orangeColor = { 1, 165/255, 0, 1 }
7+
yellowColor = { 1, 1, 0, 1 }
8+
grayColor = { 64/255, 64/255, 64/255, 1 }
9+
gray2Color = { 128/255, 128/255, 128/255, 1 }
10+
fontColor = { 192/255, 192/255, 192/255, 1 }
11+
lineColor = { 160/255, 160/255, 160/255, 1 }

dash_love2d_ios/conf.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
function love.conf( t )
3+
t.identity = "IoT"
4+
t.window.title = "Dashboad"
5+
-- 3.5" 480 X 320
6+
--t.window.width = love.graphics.getWidth()
7+
--t.window.height = love.graphics.getHeight()
8+
t.window.width = 480
9+
t.window.height = 320
10+
t.window.borderless = false
11+
t.window.resizable = true
12+
t.window.fullscreen = false -- development mode. production mode = true
13+
t.window.fsaa = 4
14+
t.window.vsync = true
15+
end

dash_love2d_ios/dash.love

5.22 KB
Binary file not shown.

dash_love2d_ios/gauge.lua

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require "color"
2+
Object = require "classic"
3+
4+
Gauge = Object:extend()
5+
6+
function Gauge:new (x, y, title, unit, value)
7+
8+
self.start_x = x or 0
9+
self.start_y = y or 0
10+
self.title = title;
11+
self.unit = unit;
12+
self.value = value or 0
13+
14+
self.gauge_height = 150
15+
self.gauge_width = 220
16+
self.minValue = 0
17+
self.maxValue = 100
18+
19+
end
20+
21+
function Gauge:setValue(newValue)
22+
self.value = newValue
23+
end
24+
25+
function Gauge:setXY(x, y)
26+
self.start_x = x
27+
self.start_y = y
28+
end
29+
30+
31+
function Gauge:draw ()
32+
33+
local gauge_y = self.gauge_height + self.start_y - 30
34+
local gauge_x = self.gauge_width / 2 + self.start_x
35+
36+
-- adjust color based on self.value
37+
if self.value < 30 then love.graphics.setColor(greenColor)
38+
elseif self.value < 50 then love.graphics.setColor(yellowColor)
39+
elseif self.value < 80 then love.graphics.setColor(orangeColor)
40+
else love.graphics.setColor(redColor)
41+
end
42+
love.graphics.arc( "fill", gauge_x, gauge_y, 80, 0, -math.pi )
43+
44+
local portion = math.pi * self.value / 100
45+
local degree = ( math.pi - portion )
46+
love.graphics.setColor(whiteColor)
47+
love.graphics.arc( "line", gauge_x, gauge_y, 80, -degree, 0 )
48+
love.graphics.arc( "fill", gauge_x, gauge_y, 80, -degree, 0 )
49+
50+
love.graphics.setColor(grayColor)
51+
love.graphics.arc( "line", gauge_x, gauge_y, 80, 0, -math.pi )
52+
love.graphics.arc( "line", gauge_x, gauge_y, 50, 0, -math.pi )
53+
love.graphics.arc( "fill", gauge_x, gauge_y, 50, 0, -math.pi )
54+
55+
-- temperature label
56+
love.graphics.setColor(fontColor)
57+
love.graphics.setNewFont(24)
58+
if self.value < 10 then love.graphics.print(self.value .. self.unit, gauge_x - 23 , gauge_y - 20)
59+
else love.graphics.print(self.value .. self.unit, gauge_x - 25 , gauge_y - 20)
60+
end
61+
-- min/max label
62+
love.graphics.setNewFont(12)
63+
love.graphics.print(self.minValue, gauge_x - 70, gauge_y + 5)
64+
love.graphics.print(self.maxValue, gauge_x + 50, gauge_y + 5)
65+
66+
end

dash_love2d_ios/label.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "color"
2+
3+
Object = require "classic"
4+
5+
Label = Object:extend()
6+
7+
function Label:new (x, y, title, unit, value)
8+
9+
self.start_x = x or 0
10+
self.start_y = y or 0
11+
self.title = title
12+
self.unit = unit
13+
self.value = value or 0
14+
15+
end
16+
17+
function Label:setValue(newValue)
18+
self.value = newValue
19+
end
20+
21+
function Label:setXY(x,y)
22+
self.start_x = x
23+
self.start_y = y
24+
end
25+
26+
function Label:draw ()
27+
28+
love.graphics.setColor(fontColor)
29+
love.graphics.setNewFont(14)
30+
love.graphics.print(self.title, self.start_x+5, self.start_y+20)
31+
32+
love.graphics.setNewFont(100)
33+
love.graphics.print(self.value, self.start_x+5, self.start_y+30)
34+
love.graphics.setNewFont(24)
35+
if self.value < 10 then love.graphics.print(self.unit, self.start_x+70, self.start_y+100)
36+
else love.graphics.print(self.unit, self.start_x+130, self.start_y+100)
37+
end
38+
39+
end

dash_love2d_ios/main.lua

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
require "color"
3+
require "panelL"
4+
require "panelR"
5+
6+
local frame = 0
7+
local width = 480
8+
local height = 320
9+
10+
function love.load()
11+
love.graphics.setColor ({ 0,0,0,1 })
12+
frame = 0
13+
panelL.init( "Current Temperature", "Living Room", "°C", 10, 15 )
14+
panelR.init( "Temperature vs. Humidity", "Temperature", "Humidity", 250, 15 )
15+
end
16+
17+
function love.update(dt)
18+
19+
-- detect iOS orientation
20+
width = love.graphics.getWidth()
21+
height = love.graphics.getHeight()
22+
23+
if width < height then
24+
-- portrait mode
25+
panelL.setXY( width/2 - 110, 15 )
26+
panelR.setXY( width/2 - 110, height/2 - 15)
27+
else
28+
-- landscape mode
29+
panelL.setXY( width/2 - 220, 15)
30+
panelR.setXY( width/2 + 15, 15)
31+
end
32+
33+
-- not time-sensitive instrument. 1 sec.
34+
frame = frame + dt
35+
local time_series_1 = {}
36+
local time_series_2 = {}
37+
local randomTemperature = 0
38+
local randomHumidity = 0
39+
40+
if frame > 1 then
41+
42+
-- randome generation : temperature, humidity (Demo)
43+
randomTemperature = math.random ( 20, 90 )
44+
randomHumidity = math.random ( 40, 70 )
45+
46+
-- Left Panel
47+
panelL.setValue ( randomTemperature )
48+
49+
-- Right Panel
50+
time_series_1, time_series_2 = panelR.getValues()
51+
if #time_series_1 > 0 then
52+
table.remove(time_series_1, 1) -- remove the first element
53+
table.insert(time_series_1, randomTemperature) -- append to time-series-array
54+
end
55+
if #time_series_2 > 0 then
56+
table.remove(time_series_2, 1) -- remove the first element
57+
table.insert(time_series_2, randomHumidity) -- append to time-series-array
58+
end
59+
panelR.setValues( time_series_1, time_series_2 )
60+
-- clear "frame"
61+
frame = 0
62+
end
63+
end
64+
65+
function love.draw()
66+
panelL.draw()
67+
panelR.draw()
68+
end

0 commit comments

Comments
 (0)