Total Pageviews

Monday, May 12, 2014

Config ទាល់តែស្លាប់

Ultimate Config
វាមានការលំបាក់នៅពេលដែល Smart Devices  ដែលមាន Resolutions ខុសៗគ្នា ហើយនៅពេលដែល build វានោះនឹងមានការលំបាកបន្តិច។ ដូចដែលអ្នកដឹងស្រាប់ហើយដូចជា ប្រភេទដែល Run android ដែលមានក្រុមហ៊ុនជាច្រើនបានបង្កើតវា។ សំរាប់ Corona ប្រើនៅ Dynamic Scaling ក្នុងការធ្វើការជាមួយនឹង devices ទាំងនោះ។ ជាមួយនឹង Dynamic Scaling  អាចប្រើនៅ Common set of screen coordinates ហើយ Corona  នឹងចាប់ផ្តើម scale នៅ text & graphics សំរាប់ resolution screens ដែលខុសគ្នា។ ដោយវាអាច scale upwards or downwards អាស្រ័យលើចំនុចចាប់ផ្តើម។ ហើយវាអាចប្រើជាមួយនឹង higher resolution images នៅពេលត្រូវការ scale up

ចាប់ផ្តើមជាមួយនឹង Basic Screen Shapes
ចូរមើលនៅ រូបដែលមាន 360 x 570 width & height។ យើងនឹងធ្វើឪ្យរូប backgrounds ធំជាង physical screen។ ហេតុអ្វី? ពីព្រោះមាន devices ជាច្រើនដែលមានទំហំខុសគ្នា។ Inner rectangle គឺ 320 x 480 block។ ជាបំណែករបស់រូបដែលស្ថិតនៅខាងលើ, ខាងក្រោម, ឆ្វេង ហើយនឹងស្តាំ ដែលនៅលើអេក្រង។ នៅពេលដែលប្តូរទៅជា tall device ដូចជា iPhone5 or most android phones វានឹងប្រើនៅ full height of the image ប៉ុន្តែ portion នៃខាងឆ្វេងហើយនឹងស្តាំ will reside off screen។ បើគិតអំពីទំហំរបស់ iPad ដែលដូចជារាងបួនជ្រុង វានឹងកាត់នៅផ្នែកខាងលើហើយនឹងខាងក្រោម ប៉ុន្តែប្រើនៅ full width of the image។ បំណែកដែលកាត់ចេញពីអេក្រងហៅថា bleed area
ពេលនេះអាចស្វែងយល់អំពីវាដើម្បីលំអិតសូមមើលនៅ config.lua ដែលវាជាស្តងដាររបស់ Corona Lua ហើយត្រូវប្រើជាមួយនឹង API ខ្លះទៀតដើម្បីបំពេញវាះ
@ read the pixel width/height of the device
@ use math for various calculations -- ប្រើគណិតសំរាប់គណនា
@ read the device model number -- អាននៅប្រភេទលេខរបស់ device
@ perform string manipulations -- បង្ហាញនៅ String
@ use conditional “if – then - else” logic -- ប្រើនៅ if then else

iPad Configuartion
ចំនុចទីមួយដែលត្រូវធ្វើគឺស្គាលប្រភេទ device បើសិនវាជា iPad។ យើងអាចទទួលនៅ model number ដោយ API ដូចនេះ
System.getInfo (“model”)
បើសិនជាអ្នក Testing នៅលើ iPad ពេលនោះ String របស់វានឹងបង្ហាញថា iPad។ ដូចនេះសូមប្រើនៅពណ័មានហើយបន្ទាបមកបញ្ចូលនៅ if statement
If ( string.sub ( system.getInfo (“ model ”), 1, 4 ) == “iPad”) then
Application =
{
Content =
{
            Width = 360,
            Height = 480,
            Scale = “letterBox”,
            xAlign = “center”,
            yAlign = “center”,
            imageSuffix =
            {
            [ “@x”] = 1.5,
            [ “@4x”] = 3.0,
            },
},
}

ការប្រើប្រាស់ឡូអា string.sub() function វានឹងអាននៅ device model ហើយស្វែងរកនៅតួអក្សរចំនួន៤។ ដូចជាការរកពាក្យ iPad បើសិនវាជាការពិតនោះវានឹងបញ្ចូលនៅកម្មវិធី content table ដែលរួមបញ្ចូលនៅ width & height parameters នៃ 360 & 480 respectively។ យើងប្រើ 360ពីព្រោះ background គឺ 360 pixels នៅប្រវែងបណ្តោយ ហើយចង់ឪ្យវា span the full width of the iPad screen។​ បើសិនជាកម្មវិធីដាក់ជា portrait ពេលនោះវានឹងធ្វើការនៅក្នុង config.lua ហើយបើសិនជាដាក់វាជា landscape mode ត្រូវដាក់ឪ្យច្បាស់នៅ width & height នៅក្នុង portrait orientation ពីព្រោះ corona នឹងរៀបចំអ្វីទាំងអស់
ជាមួយនឹង 360 ជាប្រវែងបណ្តោយ ត្រូវគិតអំពី height ដែលត្រូវពេញនឹង iPad’s screen shape។ អេក្រងរបស់ iPad គឺ 768 x 1024 or 1536 x 2048 សំរាប់ iPad retina (exactly double thus the same ratio) ទីមួយត្រូវចែក ១០២៤​%៧៦៨ = 1.33 បន្ទាប់មកស្វែងរកកំពស់ យក ១.៣៣​ X 360 = 480
 1024 / 768 = 1.33 – screen ratio
1.33 x 360 = 480

iPhone 5 Configuration
elseif ( string.sub ( system.getInfo ( “model” ), 1, 2 )== “iP” and display.pixelHeight > 960)  then
application =
{
Content =  width = 320, height = 568, scale = “letterBox”, xAlign = “center”, yAlign = “center”, imageSuffix = { [ “@2x”] = 1.5, [ “@ 4x “] = 3.0},},}
  
នៅពេលនេះ ការស្វែងរកមានតែតួអក្សរចំនួន២​នៃ model string? ហេតុអ្វី? វានឹងចាប់នៅ iPhone & iPod Touch models។ ការស្វែងរកនៅ actual pixelHeight របស់ devices។ បើសិនជា Pixel ធំជាង 960 (កំពស់របស់ iPhone4) ពេលនោះ the device is an iPhone5 or latest generation iPod Touch។ វាគឺ 640 X 1136 Device នៅពេលដែល scale it ទៅកាន់ 320 pixel ដូចនេះវាបាន 320 x 568 device
ដូចនេះ Preference គឺជា sample background ដែល height ទៅកាន់ span the screen height របស់ iPhone5។ ដែលកំពស់ដើមរបស់ iPhone 5 screen គឺ 640x1136។ នាំឪ្យបាននៅ “inner rectangle” នៅលើ background គឺ 320 pixels wide ដែលគណិតវិទ្យាយើងមានះ
320/640 = 0.5 – inner rectangle width / device pixel height = factor
1136 x 0.5 = 568 – device pixel height x factor = config.lua “height” setting

Notice that our sample background image is 570 pixels in height. As such a mere 1 pixel will “bleed” off the top and bottom of the iPhone 5 screen

iPhone 3,4 and Older iPod Touch
elseif ( string.sub (system.getInfo (“model”), 1 , 2, == “iP”)  then
application =
{
Content = {
Width = 320, height = 480, scale = “letterBox”, xAlign = “center”, yAlign = “center”, imageSuffix =
{ [“@2x “] = 1.5, [ “@4x”] = 3.0 }, },}
យើងមានកូដខាងលើទាក់ទងនឹង iPhone5 ជាមួយនឹង model string ដែលចាប់ផ្តើមជាមួយនឹង iP ដែលជាតួអក្សរចាប់ផ្តើមជាមួយនឹង iPhone & iPod Touch ជាមួយនឹង screen size 320 x 480, 640 x 960។ អាចប្រើនៅ width & height របស់ inner rectangle នៅក្នុង configuration block: 320 and 480

សំរាប់ Android, Kindle Fire & Nook
ជាចុងក្រោយយើងត្រូវបែងចែកនៅ​16:9 android devices ពី 5:3 devices ( Nook & Kindle Fire) 16:9 devices គឺជា 1.777:1 ratio។ ចំណែក Nook & Kindle Fire គឺមាន resolution 1024x600 Or 1.7:1 ដូចនេះវាអាចប្រើជាមួយនឹង pixelHeight & pixelWidth ក្នុងការគិតនៅ Ratio សំរាប់ screen size។ បើសិនជាវាធំជាង 1.72 យើងបញ្ចូលនៅ screen similar ទៅកាន់ 16:9 iPhone5, បើសិនជាមិនទេវានឹងដាក់ default to something that fits those tables better
Elseif ( display.pixelHeight / display.pixelWidth > 1.72 ) then
Application = { content = { width = 320, height = 570, scale = “letterBox”, xAlign = “center”, yAlign = “center”, imageSuffx = { [ “@2x”] = 1.5, [ “@4x”] = 3.0, },}, }
Else application = { content = { width = 320, height = 512, scale = “letterBox”, yAlign = “center”, xAlign = “center”, imageSuffix = { [“@2x”] = 1.5, [ “@4x”] = 3.0, },},} end

ចំនុចពិសេសះ
សំរាប់ហ្វាល់ config.lua នឹងមានការធ្វើនៅជាច្រើនប្រភេទ devices ហើយខាងក្រោមនេះជាចំនុចដែលត្រូវចង់ចាំះ
១. សំរាប់ devices ខ្លះបំណែករបស់ background will be off screen, so don’t put anything “critical “in the bleed areas
២. សំរាប់ object ដែលបានដាក់ទីតាំងក្នុងការ fixed numbers ដូចជា player.x = 64 & player.y = 64 តែងតែជា 64points away from the top and left edges, វាអាស្រ័យលើ device ដែល closer or further away from center។ ដូចគ្នា Objects ដែលបានដាក់ដោយការប្រើនៅ right and bottom edges will stay the prescribed distance away from those edges ( player.x = display.contentWidth – 64 will stay 64 points away from the right edge) ប៉ុន្តែវានឹងទៅកៀក or further from the center too។​ objects ដែលមានទីតាំងនៅលើ center coordinate ( player.x = display.contentCenterX + 100) will stay in the same relative position regardless of the screen shape

Dynamic Image Selection
សំរាប់គ្រប់ទាំង configuration block ដែលផ្ទុកនៅ imageSuffix table ដូចខាងក្រោមះ
imageSuffix = { [ “@2x”] = 1.5, [ “@4x”] = 3.0,},

សំរាប់ Corona SDK វានឹង handles Dynamic Image Selection។ ឧះ អ្នកមិចង់ប្រើ same image assets នៅលើ Retina iPad & iPhone 3Gs។ ដូចនេះត្រូវបង្កើតនៅ image sets យ៉ាងតិចចំនួន២ដើម្បី accommodate both “normal”& “Retina/HD”devices
ដែល entry របស់ imageSuffix table ផ្តុកនៅ២ផ្នែក
[“@2x”] – append this suffix to all images designed for those devices
Decimal value – scale factor that Corona uses to pick the proper assets

តំលៃទីមួយអាចជាឈ្មោះអ្វីក៏បានឪ្យតែងាយស្រួលសំរាប់អ្នកសរសេរវា។ ចំណែកតំលៃទី២គឺគិតដោយការប្រើតាមខាងក្រោមនេះ
Device width / config.lua “width”= scale factor
បើសិនជា scale factor នៅលើ particular device ធំជាង number specify for the entry ពេលនោះ Corona នឹងប្រើនៅ assets ទាំងនោះ។ អាចគិតថាវាជា logical entries:
[“@2x”] = 2.0
[“@4x”] = 4.0

នេះជាហ្វាល់ដែល យកចេញពី config file
600/320 = 1.875 – kindle fire and nook
640/320 = 2.0 –iPhone5
768/360 = 2.13 – iPad
800/320 = 2.5 – kindle fire HD / nexus7
1200/320 = 3.75 – Kindle fire HD 8.9
1536/360 = 4.26 – Retina iPad

បើសិនជាវាមាន​ ៤.០ ជា scale factor សំរាប់ Ritina/HD image assets, the Kindle Fire HD 8.9 នឹងមិនប្រើនៅ assets ទាំងនោះ ពីព្រោះ scale factor comes in 3.75 ហើយចង់ប្រើនៅ assets ទាំងនោះជារូបធំមួយ។ ដូចនេះនៅក្នុង confi block មានកូដដូចខាងក្រោមះ
[“@2x”] = 1.5
[“@4x”] = 3.0
វានឹងប្រាប់ដល់ Corona  ក្នុងការប្រើនៅ​ higesh resolution “@4x”images សំរាប់ Retina/HD devices ដូចជា Retina iPad, Kindle Fire HD 8.9, & Nexus 10។ សំរាប់ devices ផ្សេងនឹងជ្រើសយក “@2x” images instead, conserving texture memory which is especially limited on mobile devices

ចំនុចសំខាន់ចុងក្រោយះ ដើម្បីបង្ហាញនៅ background ចូរប្រើនៅ display.newImageRect()
Local background = display.newImageRect (“background.png”, 360, 570)
ចំពោះលេខ 360 & 570 indicate the image’s base “1x”width & height។ ប៉ុន្តែជាការអនុវត្តន៏ មិនត្រូវបញ្ចូលវាទៅក្នុង version of the image នៅក្នុង compiled project unless you are targettig lower resolution devices that require it។​

នេះជា complete file របស់ config.lua
if string.sub(system.getInfo("model"),1,4) == "iPad" then
    application =
    {
        content =
        {
            width = 360,
            height = 480,
            scale = "letterBox",
            xAlign = "center",
            yAlign = "center",
            imageSuffix =
            {
                ["@2x"] = 1.5,
                ["@4x"] = 3.0,
            },
        },
        notification =
        {
            iphone = {
                types = {
                    "badge", "sound", "alert"
                }
            }
        }
    }

elseif string.sub(system.getInfo("model"),1,2) == "iP" and display.pixelHeight > 960 then
    application =
    {
        content =
        {
            width = 320,
            height = 568,
            scale = "letterBox",
            xAlign = "center",
            yAlign = "center",
            imageSuffix =
            {
                ["@2x"] = 1.5,
                ["@4x"] = 3.0,
            },
        },
        notification =
        {
            iphone = {
                types = {
                    "badge", "sound", "alert"
                }
            }
        }
    }

elseif string.sub(system.getInfo("model"),1,2) == "iP" then
    application =
    {
        content =
        {
            width = 320,
            height = 480,
            scale = "letterBox",
            xAlign = "center",
            yAlign = "center",
            imageSuffix =
            {
                ["@2x"] = 1.5,
                ["@4x"] = 3.0,
            },
        },
        notification =
        {
            iphone = {
                types = {
                    "badge", "sound", "alert"
                }
            }
        }
    }
elseif display.pixelHeight / display.pixelWidth > 1.72 then
    application =
    {
        content =
        {
            width = 320,
            height = 570,
            scale = "letterBox",
            xAlign = "center",
            yAlign = "center",
            imageSuffix =
            {
                ["@2x"] = 1.5,
                ["@4x"] = 3.0,
            },
        },
    }
else
    application =
    {
        content =
        {
            width = 320,
            height = 512,
            scale = "letterBox",
            xAlign = "center",
            yAlign = "center",
            imageSuffix =
            {
                ["@2x"] = 1.5,
                ["@4x"] = 3.0,
            },
        },
        notification =
        {
            iphone = {
                types = {
                    "badge", "sound", "alert"
                }
            }
        }
    }
end

 


No comments:

Post a Comment