- Joined
- Apr 30, 2011
- Messages
- 359
JASS:
//========================================================================================
//
// Align Message
// -*- overcold_ice -*-
//
// -[*] Requirements:
// - JNGP
// - latest version of JassHelper
// - Ascii
// [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-ascii-190746/[/url]
//
// Provides functions to align text in game messages
//
// -[*] API:
//
// constant real MSG_X_LEFT = 0
// constant real MSG_X_CENTER = 0.46
// constant real MSG_X_RIGHT = 0.67
// x-coordinate constants used to position the block of game messages horizontally
//
// constant real FULL_MSG_Y_TOP = 0.3
// constant real FULL_MSG_Y_MIDDLE = 0.15
// constant real FULL_MSG_Y_BOTTOM = 0
// y-coordinate constants used to position the block of game messages vertically
// will only work for messages with full lines (16 lines),
// except for FULL_MSG_Y_BOTTOM
//
// constant real MSG_LINE_HEIGHT = 0.0675
// height of a line in message's y-coordinate
//
// function GetMessageAlignLeft takes string Message returns string
// function GetMessageAlignCenter takes string Message returns string
// function GetMessageAlignRight takes string Message returns string
// function GetMessageJustify takes string Message returns string
// used for horizontal alignment
// will automatically break the lines if it exceed the limit
//
// function GetMessageAlignTop takes string Message returns string
// function GetMessageAlignMiddle takes string Message returns string
// function GetMessageAlignBottom takes string Message returns string
// used for vertical alignment
// returns the original string if it has 16 lines or more
// will automatically break the lines if it exceed the limit
//
// function GetMessageYTop takes string Message returns real
// function GetMessageYMiddle takes string Message returns real
// function GetMessageYBottom takes nothing returns real
// the first two of them will return an exact y-coordinate to position the
// message block based on string Message
// the last of them will inline to FULL_MSG_Y_BOTTOM
//
//========================================================================================
library AlignMessage requires Ascii
//====================================================================================
// C A L I B R A T I O N S E C T I O N
//====================================================================================
globals
// Configurable
private constant real UNKNOWN_CHAR_SIZE = 0.010966167
// Constants
constant real MSG_X_LEFT = 0
constant real MSG_X_CENTER = 0.46
constant real MSG_X_RIGHT = 0.76
constant real FULL_MSG_Y_TOP = 0.3
constant real FULL_MSG_Y_MIDDLE = 0.15
constant real FULL_MSG_Y_BOTTOM = 0
constant real MSG_LINE_HEIGHT = 0.0675
endglobals
//====================================================================================
globals
private real array CharSize
endglobals
private module Init
private static method onInit takes nothing returns nothing
set CharSize ['A'] = 0.015288462
set CharSize ['B'] = 0.012268519
set CharSize ['C'] = 0.013802083
set CharSize ['D'] = 0.015288462
set CharSize ['E'] = 0.012268519
set CharSize ['F'] = 0.009287383
set CharSize ['G'] = 0.015288462
set CharSize ['H'] = 0.015288462
set CharSize ['I'] = 0.006172360
set CharSize ['J'] = 0.006172360
set CharSize ['K'] = 0.013802083
set CharSize ['L'] = 0.010801630
set CharSize ['M'] = 0.019875000
set CharSize ['N'] = 0.015288462
set CharSize ['O'] = 0.016843220
set CharSize ['P'] = 0.012268519
set CharSize ['Q'] = 0.018402778
set CharSize ['R'] = 0.013802083
set CharSize ['S'] = 0.012268519
set CharSize ['T'] = 0.010801630
set CharSize ['U'] = 0.015288462
set CharSize ['V'] = 0.013802083
set CharSize ['W'] = 0.021603261
set CharSize ['X'] = 0.015288462
set CharSize ['Y'] = 0.013802083
set CharSize ['Z'] = 0.013802083
set CharSize ['a'] = 0.010801630
set CharSize ['b'] = 0.012268519
set CharSize ['c'] = 0.010801630
set CharSize ['d'] = 0.012268519
set CharSize ['e'] = 0.012268519
set CharSize ['f'] = 0.007703488
set CharSize ['g'] = 0.013802083
set CharSize ['h'] = 0.012268519
set CharSize ['i'] = 0.004643692
set CharSize ['j'] = 0.006172360
set CharSize ['k'] = 0.010801630
set CharSize ['l'] = 0.004643692
set CharSize ['m'] = 0.018402778
set CharSize ['n'] = 0.012268519
set CharSize ['o'] = 0.012268519
set CharSize ['p'] = 0.012268519
set CharSize ['q'] = 0.012268519
set CharSize ['r'] = 0.007703488
set CharSize ['s'] = 0.009287383
set CharSize ['t'] = 0.007703488
set CharSize ['u'] = 0.012268519
set CharSize ['v'] = 0.012268519
set CharSize ['w'] = 0.016843220
set CharSize ['x'] = 0.012268519
set CharSize ['y'] = 0.012268519
set CharSize ['z'] = 0.010801630
set CharSize [' '] = 0.006172360
set CharSize ['!'] = 0.004643692
set CharSize ['"'] = 0.006172360
set CharSize ['#'] = 0.013802083
set CharSize ['$'] = 0.010801630
set CharSize ['%'] = 0.015288462
set CharSize ['&'] = 0.016843220
set CharSize [ 39] = 0.003095794
set CharSize ['('] = 0.006172360
set CharSize [')'] = 0.006172360
set CharSize ['*'] = 0.009287383
set CharSize ['+'] = 0.010801630
set CharSize [','] = 0.004643692
set CharSize ['-'] = 0.007703488
set CharSize ['.'] = 0.004643692
set CharSize ['/'] = 0.009287383
set CharSize ['0'] = 0.012268519
set CharSize ['1'] = 0.004643692
set CharSize ['2'] = 0.012268519
set CharSize ['3'] = 0.012268519
set CharSize ['4'] = 0.013802083
set CharSize ['5'] = 0.012268519
set CharSize ['6'] = 0.012268519
set CharSize ['7'] = 0.010801630
set CharSize ['8'] = 0.012268519
set CharSize ['9'] = 0.012268519
set CharSize [':'] = 0.004643692
set CharSize [';'] = 0.004643692
set CharSize ['<'] = 0.010801630
set CharSize ['='] = 0.010801630
set CharSize ['>'] = 0.010801630
set CharSize ['?'] = 0.010801630
set CharSize ['@'] = 0.015288462
set CharSize ['['] = 0.006172360
set CharSize [ 92] = 0.007703488
set CharSize [']'] = 0.006172360
set CharSize ['^'] = 0.010801630
set CharSize ['_'] = 0.010801630
set CharSize ['`'] = 0.007703488
set CharSize ['{'] = 0.006172360
set CharSize ['|'] = 0.004643692
set CharSize ['}'] = 0.006172360
set CharSize ['~'] = 0.010801630
endmethod
endmodule
private struct Implementer
implement Init
endstruct
private function IsColorCode takes string s returns boolean
local integer l = 1
local integer i = Char2Ascii(StringCase(SubString(s, 0, 1), false))
if i != 99 then
return false
endif
loop
exitwhen l == 9
set i = Char2Ascii(StringCase(SubString(s, l, l + 1), false))
if (i < 48 or i > 57) and (i < 97 or i > 102) then
return false
endif
set l = l + 1
endloop
return true
endfunction
private function GetStringSize takes string s returns real
local integer sl = StringLength(s)
local string ss = ""
local integer l = 0
local real r = 0
loop
exitwhen l == sl
set ss = SubString(s, l, l + 1)
if ss == "|" then
set ss = SubString(s, l + 1, l + 2)
if IsColorCode(SubString(s, l + 1, l + 10)) then
set l = l + 9
elseif StringCase(ss, false) == "r" then
set l = l + 1
elseif ss == "|" then
set l = l + 1
set r = r + CharSize ['|']
else
set r = r + CharSize ['|']
endif
elseif CharSize [Char2Ascii(ss)] != 0 then
set r = r + CharSize [Char2Ascii(ss)]
else
set r = r + UNKNOWN_CHAR_SIZE
endif
set l = l + 1
endloop
return r
endfunction
private function GetSpacing takes integer n returns string
local string r = ""
loop
exitwhen n == 0
set r = r + " "
set n = n - 1
endloop
return r
endfunction
private function GetLineAlignLeft takes string s returns string
loop
exitwhen SubString(s, 0, 1) != " "
set s = SubString(s, 1, StringLength(s))
endloop
return s
endfunction
private function GetLineAlignCenter takes string s returns string
return GetSpacing(R2I((1 - GetStringSize(s)) / (2 * CharSize [' ']))) + GetLineAlignLeft(s)
endfunction
private function GetLineAlignRight takes string s returns string
return GetSpacing(R2I((1 - GetStringSize(s)) / CharSize [' '])) + GetLineAlignLeft(s)
endfunction
private function GetLineJustify takes string s returns string
local integer ns = R2I((1 - GetStringSize(s)) / CharSize [Char2Ascii(" ")])
local integer sl = StringLength(s) - 1
local integer sc = R2I(sl / ns)
local integer l = 0
local string r = ""
if sc == 0 then
return s
endif
loop
exitwhen l == sl or ns == 0
if ns < sc then
set sc = ns
endif
set r = r + SubString(s, l, l + 1) + GetSpacing(sc)
set l = l + 1
endloop
set r = r + SubString(s, sl, sl + 1)
return r
endfunction
globals
private string array Line
private integer LineCount
endglobals
private function BreakString takes string s returns nothing
local integer sl = StringLength(s)
local integer ls = 0
local string ss = ""
local real r = 0
local integer l = 0
set LineCount = 0
loop
exitwhen l == sl
set ss = SubString(s, l, l + 1)
if ss == "|" then
set ss = SubString(s, l + 1, l + 2)
if IsColorCode(SubString(s, l + 1, l + 10)) then
set l = l + 9
elseif StringCase(ss, false) == "r" then
set l = l + 1
elseif ss == "|" then
set l = l + 1
set r = r + CharSize ['|']
else
set r = r + CharSize ['|']
endif
elseif StringCase(SubString(s, l, l + 2), false) == "\n" then
set LineCount = LineCount + 1
set Line [LineCount] = SubString(s, ls, l)
set l = l + 2
set ls = l
set r = 0
else
set r = r + GetStringSize(ss)
endif
if r > 1 then
set LineCount = LineCount + 1
set Line [LineCount] = SubString(s, ls, l)
set ls = l
set r = 0
endif
set l = l + 1
endloop
endfunction
private function GetLineBreak takes integer n returns string
local string r = ""
loop
exitwhen n == 0
set r = r + "\n"
set n = n - 1
endloop
return r
endfunction
//====================================================================================
// A P I
//====================================================================================
function GetMessageAlignBottom takes string s returns string
local integer l = 0
call BreakString(s)
set s = ""
loop
exitwhen GetLineAlignLeft(Line [LineCount]) != ""
set LineCount = LineCount - 1
endloop
loop
set l = l + 1
if l == LineCount then
set s = s + Line [l]
exitwhen true
else
set s = s + Line [l] + "\n"
endif
endloop
return s
endfunction
function GetMessageAlignMiddle takes string s returns string
local integer nb = 0
call BreakString(GetMessageAlignBottom(s))
if LineCount >= 16 then
return s
endif
set nb = (16 - LineCount) / 2
if nb == 0 then
return s
endif
return GetMessageAlignBottom(s) + GetLineBreak(nb)
endfunction
function GetMessageAlignTop takes string s returns string
local integer nb = 0
call BreakString(GetMessageAlignBottom(s))
if LineCount >= 16 then
return s
endif
set nb = (16 - LineCount)
return GetMessageAlignBottom(s) + GetLineBreak(nb)
endfunction
//! textmacro AlignMessage___CreateAPI takes FUNC
function GetMessage$FUNC$ takes string s returns string
local integer l = 0
call BreakString(s)
set s = ""
loop
set l = l + 1
if l == LineCount then
set s = s + GetLine$FUNC$(Line [l])
exitwhen true
else
set s = s + GetLine$FUNC$(Line [l]) + "\n"
endif
endloop
return s
endfunction
//! endtextmacro
//! runtextmacro AlignMessage___CreateAPI("AlignLeft")
//! runtextmacro AlignMessage___CreateAPI("AlignCenter")
//! runtextmacro AlignMessage___CreateAPI("AlignRight")
//! runtextmacro AlignMessage___CreateAPI("Justify")
function GetMessageYTop takes string s returns real
call BreakString(s)
return FULL_MSG_Y_TOP + (16 - LineCount) * MSG_LINE_HEIGHT
endfunction
function GetMessageYMiddle takes string s returns real
call BreakString(s)
return FULL_MSG_Y_MIDDLE + (16 - LineCount) * MSG_LINE_HEIGHT / 2
endfunction
function GetMessageYBottom takes nothing returns real
return FULL_MSG_Y_BOTTOM
endfunction
//====================================================================================
endlibrary
Last edited: