It looks like you're new here. If you want to get involved, click one of these buttons!
Simple library adjusts the size and position of the output image to the size and orientation of the screen.
Sorry my English is not well, so more in Russian. If you want to understand what it is doing just simple start code in second part and try to rotate your device from landscape to portrait and back. And try to change display view. The size of the blocks is always be 0,5 of width of the screen.
Данная библиотека содержит два класса с помощью которых можно прекратить беспокоиться о изменении положения экрана во время работы приложения.
Класс struct_touch позволяет перевести не редактируемые поля класса touch в редактируемую форму, а так же содержит служебные функции переноса системы координат и проверку границ для режимов отображения spriteMode( CENTER/RADIUS/CORNER)
Класс screen представляет собой простой оператор части экрана(или всего экрана). По сути он является программой прослойкой между сообщениями о касаниях и отрисовках от Codea к другим частям программы, которым эти сообщения необходимы.
Его преимущество состоит в том, что при изменении размеров экрана (поворот и так далее), относительные размеры объекта данного класса остаются неизменными, а его система координат - постоянной.
--=====================STRUCT_TOUCH===================
struct_touch = class()
function struct_touch:init(touch)
self.id = touch.id
self.x = touch.x
self.y = touch.y
self.prevX = touch.prevX
self.prevY = touch.prevY
self.deltaX = touch.deltaX
self.deltaY = touch.deltaY
self.state = touch.state
self.tapCount = touch.tapCount
end
function struct_touch:newzero(position)
self.x = self.x - position.x
self.y = self.y - position.y
end
function struct_touch:inbounds(width, height)
if spriteMode() == CENTER or spriteMode() == RADIUS then
return self.x < width/2 and self.y < height/2
else
return math.abs(self.x-width/2)<width/2 and math.abs(self.y-height/2)<height/2
end
end
--=====================SCREEN===================
screen = class()
function screen:init(WidthPercentage, HeightPercentage, Position, PositionIsFixed)
if WidthPercentage ~= nil then
self.wp = WidthPercentage
else
self.wp = 1
end
self.w = WIDTH*self.wp
if HeightPercentage ~= nil then
self.hp = HeightPercentage
else
self.hp = 1
end
self.h = HEIGHT*self.hp
self.i = image(self.w, self.h)
self.floatposition = vec2(0,0)
if PositionIsFixed == nil or PositionIsFixed then
self.fixed = true
if Position ~= nil then
self.pos = Position
else
self.pos = vec2(0,0)
end
else
if Position ~= nil then
self.floatposition = Position
end
self.pos = vec2(self.floatposition.x*WIDTH, self.floatposition.y*HEIGHT)
self.fixed = false
end
self.objects = {}
self.visible = true
self.enable = true
self.copylastframe = false
self.background = color(0,255)
self.CurentTouch = nil
end
function screen:draw()
if not self.enable or not self.visible then
return
end
if WIDTH ~= self.w/self.wp or HEIGHT ~= self.h/self.hp then
self:update()
end
pushStyle()
setContext(self.i)
if not self.copylastframe then
fill(self.background)
rect(0,0,self.w,self.h)
end
for i = 1, #self.objects do
self.objects[i]:draw()
end
setContext()
popStyle()
sprite(self.i, self.pos.x, self.pos.y)
end
function screen:touched(touch)
if not self.enable then
return
end
self.CurentTouch = struct_touch(touch)
self.CurentTouch:newzero(self.pos)
if self.CurentTouch:inbounds(self.w, self.h) then
for i = 1, #self.objects do
self.objects[i]:touched(self.CurentTouch)
end
end
end
function screen:update()
self.w = WIDTH*self.wp
self.h = HEIGHT*self.hp
self.i = image(self.w, self.h)
if not self.fixed then
self.pos = vec2(self.floatposition.x*WIDTH, self.floatposition.y*HEIGHT)
end
end
function screen:clear()
self.i = image(self.w, self.h)
self.objects = {}
self.touches = {}
self.visible = true
self.enable = true
self.background = color(0,255)
self.copylastframe = false
end
function screen:push(obj)
table.insert(self.objects, obj)
end
function screen:pop(i)
return table.remove(self.objects, i)
end
Simple app that can use this lib:
--=====================TOUCHFINDER===================
touchfinder = class()
function touchfinder:init()
self.pos = nil
self.state = nil
self.color = color(128)
end
function touchfinder:draw()
if self.pos == nil or self.state == nil then
return
end
if self.state == BEGAN then
fill(255)
elseif self.state == ENDED then
fill(0)
else
fill(self.color)
end
ellipse(self.pos.x, self.pos.y, 100)
end
function touchfinder:touched(touch)
self.pos = vec2(touch.x, touch.y)
self.state = touch.state
end
--=====================MAINFUNC===================
function setup()
spriteMode(CORNER)
scr1 = screen(0.5, nil, nil, false)
scr2 = screen(0.5, nil, vec2(0.5, 0), false)
scr1.background = color(255,0,0,10)
scr2.background = color(0,255,0,10)
tf1 = touchfinder()
tf2 = touchfinder()
tf1.color = color(255,255,0)
tf2.color = color(0,0,255)
scr1:push(tf1)
scr2:push(tf2)
end
function draw()
scr1:draw()
scr2:draw()
end
function touched(touch)
scr1:touched(touch)
scr2:touched(touch)
end
Comments
@mikewin - welcome to the forum.
There's no need to worry about your English - we have many languages here, and we know how hard it is to program in English if you don't speak it.
Thank you for sharing!