View Single Post
Old 09-17-2008, 02:26 AM   #1
Anitarf
Procrastination Incarnate


Development Director
 
Join Date: Feb 2004
Posts: 8,189

Submissions (19)

Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)

2008 Spell olympics - Fire - SilverApproved Map: Old School Alliance TacticsHero Contest #2 - 3rd PlaceSpell making session 2 winner

Default DisplayCenteredText

This library requires Table.
It was developed based on my research posted here.

Collapse JASS:
library DisplayCenteredText initializer Init requires Table

//*****************************************************************
//*  DisplayCenteredText 2.0
//*
//*  written by: Anitarf
//*
//*  A set of functions for displaying centered game messages. The
//*  functions do two things in order to center the message: first,
//*  they add spaces at the start of the message string so the
//*  message is "pushed" to the center of the area available for
//*  game messages, which is about two thirds of the screen; and
//*  second, they display the message at a certain x offset so the
//*  above mentioned area gets centered on the screen.
//*
//*  The values used were experimentally determined and are only
//*  approximations, but results are very reliable in practice in
//*  nearly all game resolution and with almost all input strings.
//*
//*  The following functions are available:
//*
//*    function GetCenteredMessage takes string line returns string
//*    function DisplayCenteredTextToPlayer takes player toPlayer, real y, string message returns nothing
//*    function DisplayTimedCenteredTextToPlayer takes player toPlayer, real y, real duration, string message returns nothing
//*
//*  Note that if the string is too long to fit in the available
//*  area then the game will split it before displaying, centering
//*  won't work in this case, the solution is to give the functions
//*  shorter strings to work with.
//*
//*  Version 2.0 ignores colour codes when calculating the length
//*  of the string so coloured strings are properly centered now.
//*****************************************************************

    globals
        private constant real DEFAULT_CHAR_WIDTH = 156 //default width for all undefined characters
        private StringTable uppercase
        private StringTable lowercase
    endglobals

    private function Init takes nothing returns nothing
        set uppercase = StringTable.create()
        set lowercase = StringTable.create()

        // experimentally determined values, in 1/10000 of max message width
        // more info: [url]http://www.wc3campaigns.net/showthread.php?t=90599[/url]
        set uppercase["a"]= 206
        set uppercase["b"]= 162
        set uppercase["c"]= 183
        set uppercase["d"]= 193
        set uppercase["e"]= 153
        set uppercase["f"]= 122
        set uppercase["g"]= 204
        set uppercase["h"]= 200
        set uppercase["i"]=  77
        set uppercase["j"]=  80
        set uppercase["k"]= 183
        set uppercase["l"]= 148
        set uppercase["m"]= 264
        set uppercase["n"]= 200
        set uppercase["o"]= 225
        set uppercase["p"]= 156
        set uppercase["q"]= 237
        set uppercase["r"]= 179
        set uppercase["s"]= 156
        set uppercase["t"]= 151
        set uppercase["u"]= 187
        set uppercase["v"]= 183
        set uppercase["w"]= 282
        set uppercase["x"]= 204
        set uppercase["y"]= 179
        set uppercase["z"]= 179

        set lowercase["a"]= 151
        set lowercase["b"]= 159
        set lowercase["c"]= 147
        set lowercase["d"]= 162
        set lowercase["e"]= 159
        set lowercase["f"]=  98
        set lowercase["g"]= 176
        set lowercase["h"]= 159
        set lowercase["i"]=  65
        set lowercase["j"]=  77
        set lowercase["k"]= 147
        set lowercase["l"]=  68
        set lowercase["m"]= 227
        set lowercase["n"]= 156
        set lowercase["o"]= 166
        set lowercase["p"]= 162
        set lowercase["q"]= 162
        set lowercase["r"]=  98
        set lowercase["s"]= 126
        set lowercase["t"]= 100
        set lowercase["u"]= 159
        set lowercase["v"]= 159
        set lowercase["w"]= 229
        set lowercase["x"]= 159
        set lowercase["y"]= 159
        set lowercase["z"]= 147

        set lowercase["."]=  51
        set lowercase[","]=  55
        set lowercase[" "]=  80
        set lowercase["-"]= 104
    endfunction

    function GetCenteredMessage takes string line returns string
        local string char
        local integer i=0
        local real charwidth
        local real totalwidth=0.0

        loop //get the approximate width of the string
            set char = SubString(line, i, i+1)
            exitwhen char == ""
            if char=="|" then //ignore colour codes
                set char=SubString(line, i+1, i+2)
                if char=="c" then
                    set i=i+9
                elseif char=="r" then
                    set i=i+1
                endif
            else //get the width of the character
                if StringCase(char, false)==char then
                    set charwidth = lowercase[char]
                else
                    set charwidth = uppercase[char]
                endif
                if charwidth == 0.0 then
                    set charwidth = DEFAULT_CHAR_WIDTH
                endif
                set totalwidth=totalwidth+charwidth
            endif
            set i = i + 1
        endloop

        set i = R2I((5000.0-totalwidth/2)/lowercase[" "]) //number of spaces needed
        set char = ""
        loop
            exitwhen i<=3 //safety margin, better add one space too few than one space too many
            set char=char+" "
            set i=i-1
        endloop
        return char+line
    endfunction
    
    function DisplayCenteredTextToPlayer takes player toPlayer, real y, string message returns nothing
        call DisplayTextToPlayer(toPlayer, 0.25, y, GetCenteredMessage(message))
    endfunction
    function DisplayTimedCenteredTextToPlayer takes player toPlayer, real y, real duration, string message returns nothing
        call DisplayTimedTextToPlayer(toPlayer, 0.25, y, duration, GetCenteredMessage(message))
    endfunction

endlibrary
__________________
Anitarf is offline   Reply With Quote
Sponsored Links - Login to hide this ad!