Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Easy to get screen edge coordinates regardless of screen scaling — Gideros Forum

Easy to get screen edge coordinates regardless of screen scaling

kredor7kredor7 Member
edited December 2013 in Code snippets
This is a quick simple class to get the edge coordinates in the different scaling modes.

Based from appcodingeasy.com/Gideros-Mobile/Ignore-Automatic-Screen-Scaling-when-positioning-objects

But put into a easy to use class.
ScreenSize = Core.class()
 
function ScreenSize:init()
	self.widthDif = math.ceil(application:getLogicalTranslateX() / application:getLogicalScaleX())
	self.heightDif = math.ceil(application:getLogicalTranslateY() / application:getLogicalScaleY())
 
	self.screenHeight = self.heightDif + application:getContentHeight()
	self.screenWidth = self.widthDif + application:getContentWidth()
end
 
function ScreenSize:getMinX()
	return -self.widthDif
end
 
function ScreenSize:getMinY()
	return -self.heightDif
end
 
function ScreenSize:getMaxX()
	return self.screenWidth
end
 
function ScreenSize:getMaxY()
	return self.screenHeight
end
 
function ScreenSize:getMinXY()
	return self:getMinX(), self:getMinY()
end
 
function ScreenSize:getMaxXY()
	return self:getMaxX(), self:getMaxY()
end
getMinXY will return the top left X,Y coordinate of the screen getMaxXY on the other hand will return the bottom right.

I am using it for my next game I am working on but I just thought I'd share it here as someone may find use for it.

Some example code to show the display
 
s = ScreenSize.new()
x,y = s:getMinXY()
w,h = s:getMaxXY()
 
local tlBox = Shape.new()
tlBox:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
tlBox:beginPath()
tlBox:moveTo(x, y)
tlBox:lineTo(x+100, y)
tlBox:lineTo(x+100, y+100) 
tlBox:lineTo(x, y+100)
tlBox:lineTo(x, y) 
tlBox:endPath()
stage:addChild(tlBox)
 
local trBox = Shape.new()
trBox:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
trBox:beginPath()
trBox:moveTo(w, y)
trBox:lineTo(w, y+100)
trBox:lineTo(w-100, y+100) 
trBox:lineTo(w-100, y)
trBox:lineTo(w, y) 
trBox:endPath()
stage:addChild(trBox)
 
local blBox = Shape.new()
blBox:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
blBox:beginPath()
blBox:moveTo(x, h)
blBox:lineTo(x+100, h)
blBox:lineTo(x+100, h-100) 
blBox:lineTo(x, h-100)
blBox:lineTo(x, h) 
blBox:endPath()
stage:addChild(blBox)
 
local brBox = Shape.new()
brBox:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
brBox:beginPath()
brBox:moveTo(w, h)
brBox:lineTo(w, h-100)
brBox:lineTo(w-100, h-100) 
brBox:lineTo(w-100, h)
brBox:lineTo(w, h) 
brBox:endPath()
stage:addChild(brBox)
 
local middBox = Shape.new()
middBox:setFillStyle(Shape.SOLID, 0x00ff00, 0.5)
middBox:beginPath()
middBox:moveTo(x+50, y+50)
middBox:lineTo(w-50, y+50)
middBox:lineTo(w-50, h-50) 
middBox:lineTo(x+50, h-50)
middBox:lineTo(x+50, y+50) 
middBox:endPath()
stage:addChild(middBox)
+1 -1 (+4 / -0 )Share on Facebook

Comments

Sign In or Register to comment.